diff --git a/2020-Freshers/goodie-dilemma/code.js b/2020-Freshers/goodie-dilemma/code.js new file mode 100644 index 0000000..ebe2649 --- /dev/null +++ b/2020-Freshers/goodie-dilemma/code.js @@ -0,0 +1,28 @@ +const prompt=require("prompt-sync")(); +var fs = require('fs'); +var input = fs.readFileSync('input.txt').toString().split('\r\n'); //Read file from input.txt +var inputObj = {}; //Convert to object +input.forEach(function(line) { + var lineArr = line.split(':'); + inputObj[lineArr[0]] = Number(lineArr[1]); +}); +const sortArray=Object.entries(inputObj).sort((a,b)=>{ //Convert object to array and sorting it in ascending order + return a[1]-b[1]; +}) +var emp=Number(prompt("Enter the number of employees ")) +var min=sortArray[emp-1][1]-sortArray[0][1] +var minIndex=0 +//Working below to find the minimum difference between goodies +for(var i=1;i{ + return `\n${item[0]}:${item[1]}` +}).join(' ') +const data=`The goodies selected for distribution are: \n${result}\n\nAnd the difference between the chosen goodie with highest price and the lowest price is ${min}` +console.log(data) +fs.writeFileSync("output.txt",data) //Write the output into output.txt file +console.log(data) \ No newline at end of file diff --git a/2020-Freshers/goodie-dilemma/input.txt b/2020-Freshers/goodie-dilemma/input.txt new file mode 100644 index 0000000..0804ffc --- /dev/null +++ b/2020-Freshers/goodie-dilemma/input.txt @@ -0,0 +1,10 @@ +Fitbit Plus:7980 +Ipods:22349 +MI Band:999 +Cult Pass:2799 +Macbook Pro:229900 +Digital Camera:11101 +Alexa:9999 +Sandwich Toaster:2195 +Microwave Oven:9800 +Scale:4999 \ No newline at end of file diff --git a/2020-Freshers/goodie-dilemma/output.txt b/2020-Freshers/goodie-dilemma/output.txt new file mode 100644 index 0000000..01fafcf --- /dev/null +++ b/2020-Freshers/goodie-dilemma/output.txt @@ -0,0 +1,6 @@ +The goodies selected for distribution are: + +Microwave Oven:9800 +Alexa:9999 + +And the difference between the chosen goodie with highest price and the lowest price is 199 \ No newline at end of file diff --git a/2020-Freshers/goodie-dilemma/sample_input.txt b/2020-Freshers/goodie-dilemma/sample_input.txt deleted file mode 100644 index 42f8c20..0000000 --- a/2020-Freshers/goodie-dilemma/sample_input.txt +++ /dev/null @@ -1,14 +0,0 @@ -Number of employees: 4 - -Goodies and Prices: - -Fitbit Plus: 7980 -IPods: 22349 -MI Band: 999 -Cult Pass: 2799 -Macbook Pro: 229900 -Digital Camera: 11101 -Alexa: 9999 -Sandwich Toaster: 2195 -Microwave Oven: 9800 -Scale: 4999 \ No newline at end of file diff --git a/2020-Freshers/goodie-dilemma/sample_output.txt b/2020-Freshers/goodie-dilemma/sample_output.txt deleted file mode 100644 index 4aaee21..0000000 --- a/2020-Freshers/goodie-dilemma/sample_output.txt +++ /dev/null @@ -1,8 +0,0 @@ -The goodies selected for distribution are: - -Fitbit Plus: 7980 -Microwave Oven: 9800 -Alexa: 9999 -Digital Camera: 11101 - -And the difference between the chosen goodie with highest price and the lowest price is 3121 \ No newline at end of file