Skip to content

Commit

Permalink
Completes level 1
Browse files Browse the repository at this point in the history
  • Loading branch information
misocho committed Nov 23, 2019
1 parent ab90e06 commit 4bf4b9f
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions level1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let totalChange = {
ones: 0,
tens: 0,
hundreds: 0
};

const getFiftys = value => {
if (value < 5) {
totalChange.ones = totalChange.ones + value * 10;
} else {
if (value % 5 === 0) {
totalChange.tens = totalChange.tens + value / 5;
} else {
totalChange.hundreds = totalChange.hundreds + parseInt(value / 5);
totalChange.ones = totalChange.ones + ((value * 10) % 50);
}
}
};

const getOnes = value => {
console.log(typeof value);
totalChange.ones = totalChange.ones + value;
};

const getHundreds = arrValue => {
if (!arrValue) {
console.log("Undefined");
totalChange.hundreds = totalChange.hundreds + 0;
} else {
toNum = parseInt(arrValue.join(""));
totalChange.hundreds = totalChange.hundreds + toNum;
}
};

const getChange = change => {
const changeArray = Array.from(String(change), Number);
const length = changeArray.length;
const ones = changeArray[length - 1];
const tens = changeArray[length - 2];
const hundreds = changeArray.slice(0, length - 2);
getOnes(ones);
getHundreds(hundreds);
getFiftys(tens);
console.log(
totalChange.hundreds + " 100 note, ",
totalChange.tens + " 50 note, " + totalChange.ones + " 1 coin"
);
};

const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});

readline.question(`What's the change? `, change => {
getChange(parseInt(change));
readline.close();
});

0 comments on commit 4bf4b9f

Please sign in to comment.