Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. /*
  2. In this project, your task is to complete the unfinished functions. Instructions accompany each function explaining what the function should do and what value it should return. You will implement logic using loops, conditional statements, arrays, and objects to return the values specified in the instructions.
  3.  
  4. The `test.js` file contains tests for all of the functions in this project. Do not change any code in `test.js`. To see the output from `test.js` and to evaluate your work, open `index.html` in Chrome and view the console in the Developer Tools. You'll know that you are finding success when all of the `console.log` statements print 'true'.
  5. */
  6.  
  7.  
  8. /********************
  9. Use the following array to complete tasks 1-3
  10. *********************/
  11. let numbers = [ 1, 12, 4, 18, 9, 7, 11, 3, 101, 5, 6 ];
  12.  
  13.  
  14. /*
  15. 1. Return the 4th element in the "numbers" array
  16. */
  17. function getFourthNum(){
  18. // Your answer here:
  19. return numbers[3];
  20. }
  21.  
  22. /*
  23. 2. Iterate over the "numbers" array. Push any numbers less than 10 onto "smallNums". Return "smallNums".
  24. */
  25. function smallNums(){
  26. let smallNums = [];
  27. // Your answer here:
  28. for(i = 0; i < numbers.length ; i++) {
  29. if (numbers[i] < 10) {
  30. smallNums.push(numbers[i]);
  31. }
  32. }
  33. return smallNums;
  34. }
  35.  
  36. /*
  37. 3. Add 12, 99, and 101 (in that order) to the end of the "numbers" array. Return the "numbers" array.
  38. */
  39. function addNums(){
  40. // Your answer here:
  41. numbers.push(12);
  42. numbers.push(99);
  43. numbers.push(101);
  44. return numbers;
  45. }
  46.  
  47.  
  48.  
  49. /********************
  50. Use the following object to complete tasks 4-6
  51. *********************/
  52.  
  53. let film = {
  54. title : "Seven Samurai",
  55. director : "Akira Kurosawa",
  56. released : 1956,
  57. runtime : 207,
  58. budget : 2000000,
  59. actors : [ "Toshiro Mifune", "Takashi Shimura", "Keiko Tsushima" ],
  60. };
  61.  
  62.  
  63. /*
  64. 4. Add a property "boxoffice" with a value of 269061 to the "film" object. Return "film".
  65. */
  66. function addBoxOffice(){
  67. // Your answer here:
  68. film.boxoffice = 269061;
  69. return film;
  70. }
  71.  
  72. /*
  73. 5. Add the name "Yukiko Shimazaki" to the "actors" array. Return "film";
  74. */
  75. function addActor(){
  76. // Your answer here:
  77. film.actors.push("Yukiko Shimazaki");
  78. return film;
  79. }
  80.  
  81. /*
  82. 6. Now that you've added a "boxoffice" property, subtract "budget" from "property" and return the difference. This number is the amount lost by the studio in making the film. Return the loss.
  83. */
  84. function getLosses(){
  85. // Your answer here:
  86. film.loss = film.boxoffice - film.budget;
  87. return film.loss;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. /*
  96. 7. Iterate over "letterVals" and "numberVals". Concatenate the values from the two arrays and store the new values in the "vals" array. Return "vals".
  97.  
  98. Your function should return [ "v5", "x67", "r34", "f456", "p78" ];
  99. */
  100.  
  101. let numberVals = [ 5, 67, 34, 456, 78 ];
  102. let letterVals = [ "v", "x", "r", "f", "p" ];
  103.  
  104. function interleave(){
  105. let vals = [];
  106. // Your answer here:
  107. for(i = 0; i < numberVals.length; i++) {
  108. vals.push(letterVals[i] + numberVals[i]);
  109. }
  110. return vals;
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /*
  119. 8. Iterate over the "first" and "second" arrays. Compare the values for both arrays. If the arrays values are the same, then store that value in the "same" array. Return "same".
  120. */
  121.  
  122. let first = [ "blink", "stand", "glasses", "chair", "numinous", "adjacent", "bracelet", "hand" ];
  123. let second = [ "think", "stand", "cheese", "break", "numinous", "mouse", "close", "toe" ];
  124.  
  125. function union(){
  126. let same = [];
  127. // Your answer here:
  128. for(i = 0; i < first.length; i++) {
  129. let temp = first[i];
  130. for(j = 0; j < second.length; j++) {
  131. if (temp === second[j]){
  132. same.push(second[j]);
  133. }
  134. }
  135. }
  136. return same;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement