Created
April 4, 2017 01:17
-
-
Save rachel-yankelevitz/fd8fd461616f1685e80a2a21ed3c6059 to your computer and use it in GitHub Desktop.
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
// Question 1 | |
// Convert every price in `prices` into a number using a function that returns an array. | |
// Store the resulting array in a variable named `numPricesArray`. You should not mutuate `prices`. | |
// To see your work, log out the string "numPricesArray" and the actual variable afterwards. | |
// function convert(){ | |
// Number(prices); | |
// console.log(convert); | |
// } | |
// WRITE QUESTION 1 ANSWER HERE | |
var prices = ['100', '125', '129', '37', '38', '75', '87', '94', '300', '301', | |
'305', '50', '0.30', '0.01', '0.5', '5', '15', '24', '35', '1041', '1', '17', | |
'21', '28', '97', '6', '10', '49', '65', '89', '6', '10', '49', '65', '89']; | |
console.log(prices); | |
var numPricesArray = prices.map(Number); | |
console.log("numPricesArray", numPricesArray); | |
// Question 2 | |
// Check that all the elements in `numPricesArray` are numbers. | |
// Your code will return a boolean, so store that boolean in a variable named `onlyPrices`. | |
// Log out the string "onlyPrices" and the actual variable. | |
// If the value of the `onlyPrices` is not true, your answer to question 1, question 2, or both is incorrect. | |
// WRITE QUESTION 2 ANSWER HERE | |
var onlyPrices = !numPricesArray.some(isNaN); | |
console.log('onlyPrices', onlyPrices); | |
// Question 3 | |
// At this point, we've confirmed that all the elements in `numPricesArray` are numbers. | |
// Now that we've done that check, we can safely compare against those elements as numbers. | |
// We're on a budget here so check if any of the prices are less than $25. Store this value in a variable named `cutoffPrice`. | |
// Store the boolean in a variable named `lowPricesPresent`, and log it out like the previous questions. | |
// Hint: Do not compare against "$25" or $25. | |
// Hint: We can inspect the array with our eyes, so `lowPricesPresent` should be true. | |
var cutOffPrice = 25; | |
console.log(cutOffPrice); | |
var lowPricesPresent = numPricesArray.some(x => x > cutOffPrice); | |
console.log('lowPricesPresent', lowPricesPresent); | |
// Question 4 | |
// Hey, we can buy things! Whoo! | |
// Let's filter out all the prices that are greater than our cutoff. | |
// (We're feeling splurgy, so our cutoff price is fair game.) | |
// Store these prices in a variable named `inBudgetPrices`. | |
// Be sure to log it out like the previous questions. | |
var inBudgetPrices = numPricesArray.filter(function(element) { | |
return(element > 25); | |
}) | |
console.log(inBudgetPrices); | |
// Question 5 | |
// The good news is we bought everything in `inBudgetPrices`. | |
// The bad news is our accountant is a huge jerk, so we can't give him the array `inBudgetPrices`. | |
// He wants a string of the prices, with each price separated by a comma and a space. | |
// Store the string of the prices in a new variable with a name of your choosing. | |
// Be sure to camelCase the variable name! Our accountant expects it. | |
var BackToString = inBudgetPrices.filter(function(element) { | |
return(element.toString() + "." + " "); | |
}); | |
console.log(BackToString); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment