Skip to content

Instantly share code, notes, and snippets.

@TCotton
Created August 12, 2017 19:36
Show Gist options
  • Save TCotton/94364ffc108112eaf04dfa685aeca438 to your computer and use it in GitHub Desktop.
Save TCotton/94364ffc108112eaf04dfa685aeca438 to your computer and use it in GitHub Desktop.
#2: I need to create a new array from a given one and transform all the elements
// Given the array of prices, return a new array with the prices n % lower.
const discount = (originalPrices, discountAmount) => {
const multiplier = 1 - discountAmount;
// we must clone the array
let result = new Array(originalPrices);
for (let i = 0; i < originalPrices.length; i++) {
result[i] = originalPrices[i] * multiplier;
}
return result;
}
const prices = [5, 25, 8, 18];
console.log(discount(prices, 0.2)); //logs [ 4, 20, 6.4, 14.4 ]
const discount = (originalPrices, discountAmount) => {
const multiplier = 1 - discountAmount;
return originalPrices.map(price => price * multiplier);
}
const prices = [5, 25, 8, 18];
console.log(discount(prices, 0.2)); // logs [ 4, 20, 6.4, 14.4 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment