Created
August 12, 2017 19:36
-
-
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
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
// 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