From 2382468e14ba03f0574fd2a0a68a95d62eb502c4 Mon Sep 17 00:00:00 2001 From: hp250497 Date: Sun, 13 Oct 2019 21:05:24 +0530 Subject: [PATCH] Added logic for issue #1 --- .../beginner/sum-of-all-numbers_hp250497.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 javascript/beginner/sum-of-all-numbers_hp250497.js diff --git a/javascript/beginner/sum-of-all-numbers_hp250497.js b/javascript/beginner/sum-of-all-numbers_hp250497.js new file mode 100644 index 0000000..a04de8d --- /dev/null +++ b/javascript/beginner/sum-of-all-numbers_hp250497.js @@ -0,0 +1,43 @@ +const readline = require("readline"); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const sum_all=(arr)=>{ + try{ + let i,sum=0; + let min=Math.min(...arr); + let max=Math.max(...arr); + for(i=min;i<=max;i++){ + sum+=i; + } + return sum; + } + catch(err){ + return(err); + } + +} + +function question(theQuestion) { + return new Promise(resolve => rl.question(theQuestion, answ => resolve(parseInt(answ)))) +} + +(async ()=>{ + try{ + var arr=[]; + arr.push(await question("Enter 1st number ")); + arr.push(await question("Enter 2nd number ")); + + console.log("Sum of the array is: "+sum_all(arr)); + process.exit(); + } + catch(err){ + console.log(err) + process.exit(1); + } + + +})(); \ No newline at end of file