JS: Change function
by kyllle
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css">
SCSS
* {
-webkit-font-smoothing: antialiased;
}
body {
padding: 5%;
}
Babel + JSX
console.clear();
// Function takes 2 params - cost, amount given
// Function should give the user back change of amount given - cost in the least amount of coins
// 1. Function called returnChange that takes 2 params totalPrice and userAmount
// 2. Create an array of available change
// 3. Create a variable that deducts totalPrice from userAmount
// - Check to make sure totalPrice and paymentValue are both numbers
// 3.1 If userAmount < totalPrice display an error
// 3.2 If userAmount > totalPrice then start a loop + check
const allowedChange = [200, 100, 50, 20, 10, 5, 2, 1]
/**
* The job of this function is to return change
* in the least amount of coins.
*
* @param {Number} totalPrice Total cost of the items
* @param {Number} paymentValue Value of users payment
* @return {Array} List of change values
*/
function returnChange(totalPrice, paymentValue) {
let price = Number(totalPrice),
payment = Number(paymentValue)
if(!price || !payment) {
console.log('Sorry, the values have to be a number')
return
}
if(payment < price) {
console.log('Sorry, you need to pay a little extra', ((payment - price) / 100))
return
}
let returnValue = payment - price
// 2.22
console.log(price, payment);
}
returnChange(142214, 345);