Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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(num){
- return parseInt(num);
- });
- // console.log( prices );
- 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(num){
- return ( typeof num==='number' /* && (num%1)===0 */ );
- });
- 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 = numPricesArray.some(function(num){
- return num < 25;
- });
- var lowPricesPresent = 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(num){
- return num < 100;
- });
- 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 jerkAccountant = inBudgetPrices.join(", ");
- console.log( jerkAccountant );
- // 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 dollarSign = inBudgetPrices.forEach(function(num, index, dollarSign){
- dollarSign[index] = "$" + dollarSign[index];
- });
- console.log("dollarSign = " + dollarSign);
- */
- var dollarSign = [];
- for( var i = 0; i < inBudgetPrices.length; i++ ){
- dollarSign.push( inBudgetPrices[i] = "$" + inBudgetPrices[i] );
- }
- console.log( "dollarSign = " + dollarSign );
- // 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 multipleDollarSigns = [];
- for( var i = 0; i < numPricesArray.length; i++ ){
- if ( numPricesArray[i] <= 10 ) {
- multipleDollarSigns.push( numPricesArray[i] = "$" + numPricesArray[i] );
- } else if ( numPricesArray[i] <= 100 ) {
- multipleDollarSigns.push( numPricesArray[i] = "$$" + numPricesArray[i] );
- } else {
- multipleDollarSigns.push( numPricesArray[i] = "$$$" + numPricesArray[i] );
- }
- }
- console.log("multipleDollarSigns = " + multipleDollarSigns);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement