Guest User

Untitled

a guest
Nov 14th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. //1. Biggie Size - Given an array, write a function that changes all positive numbers in the array to "big". Example: makeItBig([-1,3,5,-5]) returns that same array, changed to [-1, "big", "big", -5].
  2. function makeItBig(arr){
  3. for (var i=0;i<arr.length;i++){
  4. if(arr[i] > 0){
  5. arr[i] = 'big';
  6. }
  7. }
  8. return arr;
  9. }
  10.  
  11. var x = [-1,3,5,-5];
  12. console.log(makeItBig(x));
  13.  
  14. //2. Print Low, Return High - Create a function that takes array of numbers. The function should print the lowest value in the array, and return the highest value in the array.
  15. function printLowReturnHigh(arr){
  16. var lowest = arr[0];
  17. var highest = arr[0];
  18. for (var i=0;i<arr.length;i++){
  19. if (arr[i] < lowest){
  20. lowest = arr[i];
  21. }
  22. if (arr[i] > highest){
  23. highest = arr[i]
  24. }
  25. }
  26. console.log(lowest);
  27. return highest;
  28. }
  29.  
  30. var y = [-4, -7, 3, 6];
  31. console.log(printLowReturnHigh(y));
  32.  
  33. //3. Print One, Return Another - Build a function that takes array of numbers. The function should print second-to-last value in the array, and return first odd value in the array.
  34. function printAndReturn(arr){
  35. console.log(arr[arr.length-2]);
  36. for (var i=0;i<arr.length;i++){
  37. if(arr[i] % 2 !==0){
  38. return arr[i];
  39. }
  40. }
  41. }
  42.  
  43. var z = [2,8,6,3,5,9];
  44. console.log(printAndReturn(z));
  45.  
  46. //4. Double Vision - Given array, create a function to return a new array where each value in the original has been doubled. Calling double([1,2,3]) should return [2,4,6] without changing original.
  47. function double(arr){
  48. for (var i=0;i<arr.length;i++){
  49. arr[i] = arr[i]*2;
  50. }
  51. return arr;
  52. }
  53.  
  54. var x = [1,2,3];
  55. console.log(double(x));
  56.  
  57. //5. Count Positives - Given array of numbers, create function to replace last value with number of positive values. Example, countPositives([-1,1,1,1]) changes array to [-1,1,1,3] and returns it.
  58.  
  59. function countPositives(arr){
  60. var sum=0;
  61. for (var i=0;i<arr.length;i++){
  62. if (arr[i] > 0){
  63. sum++;
  64. }
  65. }
  66. arr[arr.length-1] = sum;
  67. return arr;
  68. }
  69.  
  70. var x = [-1,1,1,1];
  71. console.log(countPositives(x));
  72.  
  73. //6. Evens and Odds - Create a function that accepts an array. Every time that array has three odd values in a row, print "That's odd!". Every time the array has three evens in a row, print "Even more so!"
  74. function evenAndOdd(arr){
  75. for(var i=0;i<arr.length;i++){
  76. if(arr[i] % 2 !==0){
  77. if(arr[i+1] % 2 !==0){
  78. if(arr[i+2] % 2 !==0){
  79. console.log("That's odd!");
  80. }
  81. }
  82. }
  83. if(arr[i] % 2 == 0){
  84. if(arr[i+1] % 2 == 0){
  85. if(arr[i+2] % 2 == 0){
  86. console.log("Even more so!");
  87. }
  88. }
  89. }
  90. }
  91. }
  92.  
  93. var x = [1,5,3,2,8,6];
  94. evenAndOdd(x);
  95.  
  96. //7. Increment the Seconds - Given an array of numbers arr, add 1 to every second element, specifically those whose index is odd (arr[1], [3], [5], etc). Afterward. console.log each array value and return arr.
  97. function incrementSeconds(arr){
  98. for (var i=0;i<arr.length;i++){
  99. if(i % 2 !==0){
  100. arr[i] = arr[i] + 1;
  101. }
  102. console.log(arr[i]);
  103. }
  104. return arr;
  105. }
  106.  
  107. var x = [0,4,2,6,-1,-3];
  108. console.log(incrementSeconds(x));
  109.  
  110. //8. Previous Lengths - You are passed an array containing strings. Working within that same array, replace each string with a number - the length of the string at previous array index - and return the array. For example, previousLengths(["hello", "dojo", "awesome"]) should return ["hello", 5, 4].
  111. function previousLengths(arr){
  112. for (var i=arr.length-1;i>0;i--){
  113. arr[i] = arr[i-1].length;
  114. }
  115. return arr;
  116. }
  117.  
  118. var x = ["hello","dojo","awesome"];
  119. console.log(previousLengths(x));
  120.  
  121. //9. Add Seven to Most - Build a function that accepts array. Return a new array with all values except first, adding 7 to each. Do not alter the original array.
  122. function addSeven(arr){
  123. var newArr = [];
  124. for(var i=1;i<arr.length;i++){
  125. newArr.push(arr[i]+7);
  126. }
  127. return newArr;
  128. }
  129.  
  130. var y = [2,5,3,6];
  131. console.log(addSeven(y));
  132.  
  133. //10. Reverse Array - Given an array, write a function that reverses values, in-place. Example: reverse([3,1,6,4,2]) return same array, containing [2,4,6,1,3]. Do this without creating an empty temporary array. (Hint: you'll need to swap values).
  134. function reverse(arr){
  135. for (var i=0;i<arr.length/2;i++){
  136. var temp = arr[i];
  137. arr[i] = arr[arr.length-1 - i];
  138. arr[arr.length-1 - i] = temp;
  139. }
  140. return arr;
  141. }
  142.  
  143. var x = [2,4,6,1,3];
  144. console.log(reverse(x));
  145.  
  146. //11. Outlook: Negative - Given an array, create and return a new one containing all the values of the provided array, made negative (not simply multiplied by -1). Given [1,-3,5], return [-1,-3,-5].
  147. function allNegative(arr){
  148. var newArr = [];
  149. for (var i=0;i<arr.length;i++){
  150. newArr.push(-Math.abs(arr[i]));
  151. }
  152. return newArr;
  153. }
  154.  
  155. var y = [1,-3,5];
  156. console.log(allNegative(y));
  157.  
  158. //12. Always Hungry - Create a function that accepts an array, and prints "yummy" each time one of the values is equal to "food". If no array elements are "food", then print "I'm hungry" once.
  159. function alwaysHungry(arr){
  160. var sum = 0;
  161. for (var i=0;i<arr.length;i++){
  162. if (arr[i] == "food"){
  163. console.log("yummy");
  164. }
  165. else{
  166. sum++;
  167. }
  168. }
  169. if(sum == arr.length){
  170. console.log("I'm hungry");
  171. }
  172. }
  173.  
  174. var x = [1,2,4,6];
  175. var y = [1, "food", 1, "food"];
  176. alwaysHungry(x);
  177. alwaysHungry(y);
  178.  
  179. //13. Swap Toward the Center - Given array, swap first and last, third and third-to-last, etc. Input[true,42,"Ada",2,"pizza"] becomes ["pizza", 42, "Ada", 2, true]. Change [1,2,3,4,5,6] to [6,2,4,3,5,1].
  180. var array = [true,42,"Ada",2,"pizza"];
  181. var nums = [1,2,3,4,5,6];
  182. function swapTowardCenter(arr){
  183. var temp = arr[0];
  184. arr[0] = arr[arr.length-1];
  185. arr[arr.length-1] = temp;
  186. var temp2 = arr[2];
  187. arr[2] = arr[arr.length-3];
  188. arr[arr.length-3] = temp2;
  189. return arr;
  190. }
  191. console.log(swapTowardCenter(array));
  192. console.log(swapTowardCenter(nums));
  193.  
  194. //14. Scale the Array - Given an array arr and a number num, multiply all values in arr by num, and return the changed array arr. For example, scaleArray([1,2,3],3) should return [3,6,9].
  195. function scaleArray(arr,num){
  196. for (var i=0;i<arr.length;i++){
  197. arr[i] = arr[i]*num;
  198. }
  199. return arr;
  200. }
  201.  
  202. var array = [1,2,3];
  203. var number = 3;
  204. console.log(scaleArray(array,number));
Add Comment
Please, Sign In to add comment