Created
March 31, 2017 03:22
-
-
Save bmacmill/eb09e6be76a60e44f1d570532214d7e0 to your computer and use it in GitHub Desktop.
homework 01 arrays
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
var numPricesArray = []; | |
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']; | |
prices.forEach(function(num){ | |
var newOne = parseFloat(num); | |
numPricesArray.push(newOne); | |
}); | |
console.log("numPricesArray", numPricesArray); | |
var onlyPrices = isNaN(numPricesArray) ? "true" : "false"; | |
console.log("onlyPrices", onlyPrices); | |
var cutoffPrice = numPricesArray.filter(function(num){ | |
return num < 25; | |
}); | |
console.log("cutoffPrice", cutoffPrice); | |
var lowPricesPresent = (cutoffPrice) ? "true" : "false"; | |
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 = cutoffPrice.filter(function(element){ | |
return element < 10; | |
}); | |
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 hugeJerk = inBudgetPrices.toString(); | |
console.log(hugeJerk); | |
// 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 extraArray = []; | |
inBudgetPrices.forEach(function(elem){ | |
var extraCredit = ("$" + elem); | |
extraArray.push(extraCredit); | |
}); | |
console.log("extraArray", extraArray); | |
typeof(extraArray); | |
//an object | |
// 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 | |
//ran out of time... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Brian, here are some thoughts.
Question 1
I actually dig how you used
parseFloat()
method. This definitely works and nicely done! And just for FYI, let me show you a very short way of doing this.This should give you back the same result if you console.log() out
numPricesArray
.Question 2
Let's be careful about using isNaN() method. If you look at the documention (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN), it actually states that this checks to see if the value is an actual value
NaN
, not testing to see if the value is anumber
What you want here is using
typeof
operator to check for the type of your varible.Question 3
What you're doing here is actually checking to see if
cutOffPrice
exists or not. And it definitely does! However, it doesn't actually do what we want. You want to check to see if any item in the array is less the value of 25.Question 4
You were close. But the value you're comparing against is 25, not 10. :)
Let me know anytime if you have more questions.
-Stephen