Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. // Homework 1
  2.  
  3. // Hint: You may need SOME of these array iterator methods:
  4. // .map(), .filter(), .forEach() , .some(), .every()
  5. // Hint: You may also need SOME of the following methods:
  6. // Number(), .reverse(), typeof(), .join()
  7.  
  8. // Let's say we have an array of prices named `prices`.
  9. var prices = ['100', '125', '129', '37', '38', '75', '87', '94', '300', '301',
  10. '305', '50', '0.30', '0.01', '0.5', '5', '15', '24', '35', '1041', '1', '17',
  11. '21', '28', '97', '6', '10', '49', '65', '89', '6', '10', '49', '65', '89'];
  12.  
  13. // Question 1
  14. // Convert every price in `prices` into a number using a function that returns an array.
  15. // Store the resulting array in a variable named `numPricesArray`. You should not mutuate `prices`.
  16. // To see your work, log out the string "numPricesArray" and the actual variable afterwards.
  17.  
  18. // WRITE QUESTION 1 ANSWER HERE
  19.  
  20. var numPricesArray = prices.map(function(num){
  21. return parseInt(num);
  22. });
  23. // console.log( prices );
  24. console.log( "numPricesArray = " + numPricesArray );
  25.  
  26. // Question 2
  27. // Check that all the elements in `numPricesArray` are numbers.
  28. // Your code will return a boolean, so store that boolean in a variable named `onlyPrices`.
  29. // Log out the string "onlyPrices" and the actual variable.
  30. // If the value of the `onlyPrices` is not true, your answer to question 1, question 2, or both is incorrect.
  31.  
  32. // WRITE QUESTION 2 ANSWER HERE
  33.  
  34. var onlyPrices = numPricesArray.every(function(num){
  35. return ( typeof num==='number' /* && (num%1)===0 */ );
  36. });
  37. console.log( "onlyPrices = " + onlyPrices );
  38.  
  39. // Question 3
  40. // At this point, we've confirmed that all the elements in `numPricesArray` are numbers.
  41. // Now that we've done that check, we can safely compare against those elements as numbers.
  42. // 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`.
  43. // Store the boolean in a variable named `lowPricesPresent`, and log it out like the previous questions.
  44. // Hint: Do not compare against "$25" or $25.
  45. // Hint: We can inspect the array with our eyes, so `lowPricesPresent` should be true.
  46.  
  47. // WRITE QUESTION 3 ANSWER HERE
  48.  
  49. var cutOffPrice = numPricesArray.some(function(num){
  50. return num < 25;
  51. });
  52. var lowPricesPresent = cutOffPrice;
  53. console.log( "lowPricesPresent = " + lowPricesPresent );
  54.  
  55. // Question 4
  56. // Hey, we can buy things! Whoo!
  57. // Let's filter out all the prices that are greater than our cutoff.
  58. // (We're feeling splurgy, so our cutoff price is fair game.)
  59. // Store these prices in a variable named `inBudgetPrices`.
  60. // Be sure to log it out like the previous questions.
  61.  
  62. // WRITE QUESTION 4 ANSWER HERE
  63.  
  64. var inBudgetPrices = numPricesArray.filter(function(num){
  65. return num < 100;
  66. });
  67. console.log( "inBudgetPrices = " + inBudgetPrices );
  68.  
  69. // Question 5
  70. // The good news is we bought everything in `inBudgetPrices`.
  71. // The bad news is our accountant is a huge jerk, so we can't give him the array `inBudgetPrices`.
  72. // He wants a string of the prices, with each price separated by a comma and a space.
  73. // Store the string of the prices in a new variable with a name of your choosing.
  74. // Be sure to camelCase the variable name! Our accountant expects it.
  75.  
  76. // WRITE QUESTION 5 ANSWER HERE
  77.  
  78. var jerkAccountant = inBudgetPrices.join(", ");
  79. console.log( jerkAccountant );
  80.  
  81. // Extra Credit 1 (OPTIONAL)
  82. // Create a new array that has "$" prepended before each price in `inBudgetPrices`.
  83. // What is the type of the elements in `inBudgetPrices` now?
  84.  
  85. // WRITE EXTRA CREDIT 1 ANSWER HERE
  86. /*
  87. var dollarSign = inBudgetPrices.forEach(function(num, index, dollarSign){
  88. dollarSign[index] = "$" + dollarSign[index];
  89. });
  90. console.log("dollarSign = " + dollarSign);
  91. */
  92. var dollarSign = [];
  93. for( var i = 0; i < inBudgetPrices.length; i++ ){
  94. dollarSign.push( inBudgetPrices[i] = "$" + inBudgetPrices[i] );
  95. }
  96. console.log( "dollarSign = " + dollarSign );
  97.  
  98.  
  99. // Extra Credit 2
  100. // Create a new array based off of `numPricesArray` that has:
  101. // * one "$" prepended before a price that is greater than or equal to 0
  102. // * two "$" prepended before a price that is greater than or equal to 10
  103. // * three "$" prepended before a price that is greater than or equal to 100
  104.  
  105. // WRITE EXTRA CREDIT 2 ANSWER HERE
  106.  
  107. var multipleDollarSigns = [];
  108. for( var i = 0; i < numPricesArray.length; i++ ){
  109. if ( numPricesArray[i] <= 10 ) {
  110. multipleDollarSigns.push( numPricesArray[i] = "$" + numPricesArray[i] );
  111. } else if ( numPricesArray[i] <= 100 ) {
  112. multipleDollarSigns.push( numPricesArray[i] = "$$" + numPricesArray[i] );
  113. } else {
  114. multipleDollarSigns.push( numPricesArray[i] = "$$$" + numPricesArray[i] );
  115. }
  116. }
  117. console.log("multipleDollarSigns = " + multipleDollarSigns);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement