Skip to content

Instantly share code, notes, and snippets.

@donoage
Forked from rachel-yankelevitz/Homework 01
Last active April 2, 2017 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donoage/c21440b3dbf9510f2d4eece154f90211 to your computer and use it in GitHub Desktop.
Save donoage/c21440b3dbf9510f2d4eece154f90211 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.
/* Stephen: For best practice, be sure to remove any unused code :) */
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)
// });
/* Stephen: Sweet! Digging the shorted version! */
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
/* Stephen: I believe this will return false and would not be correct
Try something like this using .every() method
var onlyPrices = numPricesArray.every(function(num) {
return typeof num === 'number';
});
onlyPrices variable will then return a boolean value true!
*/
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.
/* Stephen: Hint. Use .some() method here */
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment