Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ## ARRAYS ## //
- console.log ("##ARRAYS##");
- console.log ("==============");
- // logEach //
- console.log ("logEach");
- console.log ("==============");
- // Write a function logEach(array) that prints every element of the array and
- // its index to the console . Example:
- // > logEach(["Anthony", "John", "Carson"]);
- // 0: Anthony
- // 1: John
- // 2: Carson
- var logEach = function (array) {
- for ( var i = 0; i < array.length; i += 1 ) {
- console.log( i + ":" + array[i]);
- }
- }
- console.log (["Anthony", "John", "Carson"]);
- logEach(["Anthony", "John", "Carson"]);
- console.log ("==============");
- // maxValue
- console.log ("maxValue");
- console.log ("==============");
- //Write a function maxValue(array) that returns the largest value in the array.
- //Assume array is an array of numbers.
- // > maxValue([12, 6, 43, 2])
- // 43
- // > maxValue([])
- // null
- // > maxValue([-4, -10, 0.43])
- // 0.43
- var maxValue = function (array) {
- var biggestNumber = null;
- for ( var i = 1; i < array.length; i += 1 ) {
- var currentNumber = array[i];
- // QUESTION 1
- // why "largest === numb" in the if statemnt necessary as per the solution?)
- if ( biggestNumber < currentNumber ) {
- biggestNumber = currentNumber
- }
- }
- // QUESTION 2
- // soultion says to use return instead of console.log, why?
- console.log(biggestNumber);
- }
- // var result = maxValue([12, 6, 43, 2]);
- // console.log(result);
- console.log("Q1: [12, 6, 43, 2]")
- maxValue([12, 6, 43, 2]);
- console.log("Q2: []");
- maxValue([]);
- console.log("Q3: [-4, -10, 0.43]");
- maxValue([-4, -10, 0.43]);
- console.log ("==============");
- // printRange
- console.log ("printRange");
- console.log ("==============");
- // Write a function printRange(start, end) that prints all the numbers from
- // start to end. If a range doesn't exist (start > end), then print "Bad Range" instead. Example:
- // > printRange(22, 24)
- // 22
- // 23
- // 24
- // > printRange(5, 1)
- // Bad Range
- var printRange = function (start, end) {
- if(start > end) {
- console.log("Bad Range");
- }
- // QUESTION 3
- // Solution says else is not necessary, why?
- else {
- for (var i = start; i <= end; i += 1) {
- console.log(i)
- }
- }
- }
- console.log("Q1: (22, 24)");
- printRange(22, 24);
- console.log("Q2: (5, 1)");
- printRange(5, 1);
- console.log ("==============");
- // ## FUNCTIONS ## //
- console.log ("##FUNCTIONS##");
- console.log ("==============");
- // isPrime //
- console.log ("isPrime");
- console.log ("==============");
- // Write a function isPrime(number) that returns a boolean indicating if
- // number is prime or not. Assume number is a positive integer.
- // > isPrime(2)
- // true
- // >isPrime(1693)
- // true
- // > isPrime(15)
- // false
- // > isPrime(303212)
- // false
- var isPrime = function(number) {
- if (number < 2) {
- return false
- }
- //QUESTION 5
- // why is else not necessary?
- else {
- for (var i = 2; i < number; i += 1){
- if (number % i === 0) {
- return false
- }
- }
- }
- return true
- }
- //QUESTION 4
- //Is there a way to incorporate console.log into the function?
- //so that it will log the result when we run it?
- console.log("Q1: isPrime(2)");
- console.log(isPrime(2));
- console.log("Q2: isPrime(1693)");
- console.log(isPrime(1693))
- console.log("Q3: isPrime(15)");;
- console.log(isPrime(15));
- console.log("Q4: isPrime(303212)");
- console.log(isPrime(303212));
- console.log ("==============");
- // firstNPrimes //
- console.log ("firstNPrimes");
- console.log ("==============");
- // Using isPrime, write a function firstNPrimes(n) that returns an array
- // of the first n prime numbers.
- // > firstNPrimes(0)
- // []
- // > firstNPrimes(1)
- // [2]
- // > firstNPrimes(4)
- // [2, 3, 5, 7]
- var firstNPrimes = function(n) {
- var primes = [];
- var number = 2;
- // QUESTION 6
- // Is there a way to write this using For loop?
- // for (var i = n; i > primes.length; i += 1){
- // if (isPrime(i)) {
- // primes.push(i);
- // }
- // }
- while (primes.length < n) {
- if (isPrime(number)) {
- primes.push(number);
- }
- number += 1;
- }
- // QUESTION 7
- // solution uses return why can't we use console.log instead?
- console.log(primes);
- }
- console.log("Q1: firstNPrimes(0)");
- firstNPrimes(0);
- console.log("Q2: firstNPrimes(1)");
- firstNPrimes(1);
- console.log("Q3: firstNPrimes(4)");
- firstNPrimes(4);
- console.log ("==============");
- // // primesUpToN //
- //
- // console.log ("primesUpToN");
- // console.log ("==============");
- //
- // function primesUpToN(n) {
- // var primeArr = [];
- //
- // for (var i = 2; i <= n; i++) {
- // if (isPrime(i)) {
- // primeArr.push(i);
- // }
- // }
- // console.log(primeArr);
- // }
- //
- // primesUpToN(4);
- // sumOfNPrimes //
- console.log ("sumOfNPrimes");
- console.log ("==============");
- // Using firstNPrimes, write a function sumOfNPrimes(n) that returns
- // the sum of the first n prime numbers.
- // > sumOfNPrimes(0)
- // 0
- // > sumOfNPrimes(1)
- // 2
- // > sumOfNPrimes(4)
- // 17
- var sumOfNPrimes = function(n) {
- var sum = 0;
- var primesArr = firstNPrimes(n);
- for(var i = 0; i < primesArr.length; i += 1) {
- sum += primesArr[i];
- }
- console.log(sum);
- }
- console.log("Q1: sumOfNPrimes(0)");
- sumOfNPrimes(0);
- console.log("Q2: sumOfNPrimes(1)");
- sumOfNPrimes(1);
- console.log("Q3: sumOfNPrimes(4)");
- sumOfNPrimes(4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement