-
-
Save liron-navon/4215b84a8294de39f1f06a569b5ad114 to your computer and use it in GitHub Desktop.
Solution for the isAdmissibleOverpayment challenge in code signal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isAdmissibleOverpayment(prices, notes, x) { | |
const difference = prices.reduce((sum, currentPrice, index) => { | |
const percentageChange = parsePercentageFromNote(notes[index]); | |
const originalPrice = reversePercentage(currentPrice, percentageChange); | |
// difference between the prices | |
return sum = sum + currentPrice - originalPrice; | |
}, 0); | |
return difference.toPrecision(5) <= x; | |
} | |
// reverse percentage implementation | |
function reversePercentage(currentPrice, percentageChange) { | |
// (1) Work out the current price as a percentage of the original price | |
const percentageFromOriginalPrice = 100 + percentageChange; | |
// (2) Find 1% by dividing the current price | |
const onePercent = currentPrice / percentageFromOriginalPrice; | |
// (3) Multiply this 1% by 100 to find the original price | |
return onePercent * 100; | |
} | |
// get the percentage from the text note | |
function parsePercentageFromNote(note) { | |
const tokens = note.split(' '); | |
if (tokens[0] === 'Same') { | |
return 0 | |
} | |
const multipier = tokens[1] === 'higher' ? +1 : -1; | |
return multipier * parseFloat(tokens[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment