Advertisement
Guest User

Untitled

a guest
May 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. // ## ARRAYS ## //
  2. console.log ("##ARRAYS##");
  3. console.log ("==============");
  4. // logEach //
  5. console.log ("logEach");
  6. console.log ("==============");
  7. // Write a function logEach(array) that prints every element of the array and
  8. // its index to the console . Example:
  9.  
  10. // > logEach(["Anthony", "John", "Carson"]);
  11. // 0: Anthony
  12. // 1: John
  13. // 2: Carson
  14.  
  15. var logEach = function (array) {
  16. for ( var i = 0; i < array.length; i += 1 ) {
  17.  
  18. console.log( i + ":" + array[i]);
  19. }
  20. }
  21. console.log (["Anthony", "John", "Carson"]);
  22. logEach(["Anthony", "John", "Carson"]);
  23.  
  24. console.log ("==============");
  25.  
  26. // maxValue
  27.  
  28. console.log ("maxValue");
  29. console.log ("==============");
  30.  
  31. //Write a function maxValue(array) that returns the largest value in the array.
  32. //Assume array is an array of numbers.
  33.  
  34. // > maxValue([12, 6, 43, 2])
  35. // 43
  36.  
  37. // > maxValue([])
  38. // null
  39.  
  40. // > maxValue([-4, -10, 0.43])
  41. // 0.43
  42.  
  43.  
  44. var maxValue = function (array) {
  45. var biggestNumber = null;
  46.  
  47. for ( var i = 1; i < array.length; i += 1 ) {
  48. var currentNumber = array[i];
  49.  
  50. // QUESTION 1
  51. // why "largest === numb" in the if statemnt necessary as per the solution?)
  52.  
  53. if ( biggestNumber < currentNumber ) {
  54. biggestNumber = currentNumber
  55. }
  56. }
  57.  
  58. // QUESTION 2
  59. // soultion says to use return instead of console.log, why?
  60. console.log(biggestNumber);
  61. }
  62.  
  63. // var result = maxValue([12, 6, 43, 2]);
  64. // console.log(result);
  65.  
  66. console.log("Q1: [12, 6, 43, 2]")
  67. maxValue([12, 6, 43, 2]);
  68. console.log("Q2: []");
  69. maxValue([]);
  70. console.log("Q3: [-4, -10, 0.43]");
  71. maxValue([-4, -10, 0.43]);
  72.  
  73.  
  74. console.log ("==============");
  75.  
  76.  
  77. // printRange
  78.  
  79. console.log ("printRange");
  80. console.log ("==============");
  81.  
  82.  
  83. // Write a function printRange(start, end) that prints all the numbers from
  84. // start to end. If a range doesn't exist (start > end), then print "Bad Range" instead. Example:
  85.  
  86. // > printRange(22, 24)
  87. // 22
  88. // 23
  89. // 24
  90.  
  91. // > printRange(5, 1)
  92. // Bad Range
  93.  
  94. var printRange = function (start, end) {
  95. if(start > end) {
  96. console.log("Bad Range");
  97. }
  98.  
  99. // QUESTION 3
  100. // Solution says else is not necessary, why?
  101.  
  102. else {
  103. for (var i = start; i <= end; i += 1) {
  104. console.log(i)
  105. }
  106. }
  107. }
  108.  
  109. console.log("Q1: (22, 24)");
  110. printRange(22, 24);
  111. console.log("Q2: (5, 1)");
  112. printRange(5, 1);
  113. console.log ("==============");
  114.  
  115.  
  116. // ## FUNCTIONS ## //
  117.  
  118. console.log ("##FUNCTIONS##");
  119. console.log ("==============");
  120.  
  121. // isPrime //
  122.  
  123. console.log ("isPrime");
  124. console.log ("==============");
  125.  
  126. // Write a function isPrime(number) that returns a boolean indicating if
  127. // number is prime or not. Assume number is a positive integer.
  128.  
  129. // > isPrime(2)
  130. // true
  131.  
  132. // >isPrime(1693)
  133. // true
  134.  
  135. // > isPrime(15)
  136. // false
  137.  
  138. // > isPrime(303212)
  139. // false
  140.  
  141. var isPrime = function(number) {
  142. if (number < 2) {
  143. return false
  144. }
  145.  
  146. //QUESTION 5
  147. // why is else not necessary?
  148.  
  149. else {
  150. for (var i = 2; i < number; i += 1){
  151. if (number % i === 0) {
  152. return false
  153. }
  154. }
  155. }
  156. return true
  157. }
  158.  
  159. //QUESTION 4
  160. //Is there a way to incorporate console.log into the function?
  161. //so that it will log the result when we run it?
  162.  
  163. console.log("Q1: isPrime(2)");
  164. console.log(isPrime(2));
  165. console.log("Q2: isPrime(1693)");
  166. console.log(isPrime(1693))
  167. console.log("Q3: isPrime(15)");;
  168. console.log(isPrime(15));
  169. console.log("Q4: isPrime(303212)");
  170. console.log(isPrime(303212));
  171.  
  172. console.log ("==============");
  173.  
  174.  
  175.  
  176.  
  177. // firstNPrimes //
  178.  
  179. console.log ("firstNPrimes");
  180. console.log ("==============");
  181.  
  182. // Using isPrime, write a function firstNPrimes(n) that returns an array
  183. // of the first n prime numbers.
  184.  
  185. // > firstNPrimes(0)
  186. // []
  187.  
  188. // > firstNPrimes(1)
  189. // [2]
  190.  
  191. // > firstNPrimes(4)
  192. // [2, 3, 5, 7]
  193.  
  194.  
  195. var firstNPrimes = function(n) {
  196. var primes = [];
  197. var number = 2;
  198.  
  199.  
  200. // QUESTION 6
  201. // Is there a way to write this using For loop?
  202.  
  203. // for (var i = n; i > primes.length; i += 1){
  204. // if (isPrime(i)) {
  205. // primes.push(i);
  206. // }
  207. // }
  208.  
  209. while (primes.length < n) {
  210. if (isPrime(number)) {
  211. primes.push(number);
  212. }
  213.  
  214. number += 1;
  215. }
  216.  
  217. // QUESTION 7
  218. // solution uses return why can't we use console.log instead?
  219. console.log(primes);
  220. }
  221.  
  222. console.log("Q1: firstNPrimes(0)");
  223. firstNPrimes(0);
  224. console.log("Q2: firstNPrimes(1)");
  225. firstNPrimes(1);
  226. console.log("Q3: firstNPrimes(4)");
  227. firstNPrimes(4);
  228.  
  229. console.log ("==============");
  230.  
  231. // // primesUpToN //
  232. //
  233. // console.log ("primesUpToN");
  234. // console.log ("==============");
  235. //
  236. // function primesUpToN(n) {
  237. // var primeArr = [];
  238. //
  239. // for (var i = 2; i <= n; i++) {
  240. // if (isPrime(i)) {
  241. // primeArr.push(i);
  242. // }
  243. // }
  244. // console.log(primeArr);
  245. // }
  246. //
  247. // primesUpToN(4);
  248.  
  249.  
  250. // sumOfNPrimes //
  251.  
  252. console.log ("sumOfNPrimes");
  253. console.log ("==============");
  254.  
  255. // Using firstNPrimes, write a function sumOfNPrimes(n) that returns
  256. // the sum of the first n prime numbers.
  257.  
  258. // > sumOfNPrimes(0)
  259. // 0
  260.  
  261. // > sumOfNPrimes(1)
  262. // 2
  263.  
  264. // > sumOfNPrimes(4)
  265. // 17
  266.  
  267.  
  268. var sumOfNPrimes = function(n) {
  269. var sum = 0;
  270. var primesArr = firstNPrimes(n);
  271.  
  272. for(var i = 0; i < primesArr.length; i += 1) {
  273. sum += primesArr[i];
  274. }
  275.  
  276. console.log(sum);
  277. }
  278.  
  279. console.log("Q1: sumOfNPrimes(0)");
  280. sumOfNPrimes(0);
  281. console.log("Q2: sumOfNPrimes(1)");
  282. sumOfNPrimes(1);
  283. console.log("Q3: sumOfNPrimes(4)");
  284. sumOfNPrimes(4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement