Skip to content

Instantly share code, notes, and snippets.

@jamesgarrett
Created October 11, 2017 03:29
Show Gist options
  • Save jamesgarrett/cacb23c98edbce539acc2d6d710be98c to your computer and use it in GitHub Desktop.
Save jamesgarrett/cacb23c98edbce539acc2d6d710be98c to your computer and use it in GitHub Desktop.
Homework 1
// 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.
// WRITE QUESTION 1 ANSWER HERE
var numPricesArray = prices.map(function(price){
return Number(price);
});
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.every(function(elem){
return typeof elem;
});
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;
var lowPricesPresent = numPricesArray.some(function(elem){
return elem < 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.
// WRITE QUESTION 4 ANSWER HERE
var inBudgetPrices = numPricesArray.filter(function(elem){
return elem < cutoffPrice;
});
console.log("inBudgetPrices", 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.
// WRITE QUESTION 5 ANSWER HERE
var thingsWeBought = inBudgetPrices.join(", ");
console.log("thingsWeBought", thingsWeBought)
// 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
var budget = inBudgetPrices.map(function(elem){
return "$" + elem;
});
console.log("budget", budget);
// Elements in inBudgetPrices are still numbers because they have not been mutated, we just mapped it
// 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
var priceRatings = inBudgetPrices.map(function(elem){
switch (true){
case (elem >= 100):
return "$$$" + elem;
break;
case (elem >= 10):
return "$$" + elem;
break;
default:
return "$" + elem;
}
});
console.log("priceRatings", priceRatings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment