Skip to content

Instantly share code, notes, and snippets.

@rachel-yankelevitz
Created March 31, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rachel-yankelevitz/1c2df418a303e6490bd070a78729464c to your computer and use it in GitHub Desktop.
Save rachel-yankelevitz/1c2df418a303e6490bd070a78729464c to your computer and use it in GitHub Desktop.
// Homework 1
// Hint: You may need SOME of these array iterator methods:
// .map(), .filter(), .forEach() , .some(), .every()
// Hint: You may also need SOME of the following methods:
// Number(), .reverse(), typeof(), .join()
// Let's say we have an array of prices named `prices`.
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'];
// 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 numArray = prices.forEach(function(x) {
// console.log(Number(x));
// // return Number(x)
// });
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 = typeof(numPricesArray) === 'number';
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.
// WRITE QUESTION 3 ANSWER HERE
var cutOffPrice = 25;
lowPricesPresent >== cutoffPrice
// 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.
// WRITE QUESTION 4 ANSWER HERE
// 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.
// WRITE QUESTION 5 ANSWER HERE
// Extra Credit 1 (OPTIONAL)
// Create a new array that has "$" prepended before each price in `inBudgetPrices`.
// What is the type of the elements in `inBudgetPrices` now?
// WRITE EXTRA CREDIT 1 ANSWER HERE
// Extra Credit 2
// Create a new array based off of `numPricesArray` that has:
// * one "$" prepended before a price that is greater than or equal to 0
// * two "$" prepended before a price that is greater than or equal to 10
// * three "$" prepended before a price that is greater than or equal to 100
// WRITE EXTRA CREDIT 2 ANSWER HERE
@donoage
Copy link

donoage commented Apr 2, 2017

Hi Rachel, check out my comments here. My comments start with Stephen in front of them. Be sure to let me know if you need help completing the work next time :)

https://gist.github.com/donoage/c21440b3dbf9510f2d4eece154f90211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment