DEV Community

Randy Rivera
Randy Rivera

Posted on

Algorithms Scripting Notes and Examples: Part 4

  • 7:45 AM Trying to do my studies early in the morning and some in the afternoon. In order for me to become who I wanna be I need to continue not giving up no matter how hard.
  • Anyways moving on. Now Today we're figuring out how to return the sum of all odd Fibonacci Numbers that are less than or equal to num.
  • Basically its just every additional number in the sequence is the sum of the previous numbers. Ex: the first two numbers in the Fibonacci sequence starts with 1 and 1. After it should be followed by 2, 3, 5, 8, and so forth.

  • For example, sum(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.

function sum(num) {
  return num;
}

sum(4); // this should return 5 because all odd Fibonacci numbers less than or equal to `4` are 1, 1, 3
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function sum(num) {
  let sequence = [0, 1]
  let count = sequence[sequence.length - 2] + sequence[sequence.length - 1];


    while (count <= num) {
     sequence.push(count);
     count = sequence[sequence.length - 2] + sequence[sequence.length - 1];
    }

     let sumOfAllOdds = 0

    sequence.forEach(function(num) {
    if (num % 2 != 0) {
      sumOfAllOdds += num; 
  }
    });

  return sumOfAllOdds;
}

console.log(sum(4)); // want to return 5 because that's the sum of all odd Fibonacci numbers [ 1, 1, 3];
Enter fullscreen mode Exit fullscreen mode

Alright onto the next one! This time they want us to check return the sum of all prime numbers that are less than or equal to num.

  • If you don't know what a prime number is, basically it's a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. While something like 4 is not because it is divisible by 1, 2 and 4.
  • Now lets rewrite SumOfAllPrimes so it returns the sum of all prime numbers that are less than or equal to num.
function sumOfAllPrimes(num) {
  return num;
}

sumOfAll(10);
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function sumOfAllPrimes(num) {
  function isPrime(num) {
    for (let x = 2; x < num; x++) {
      if (num % x === 0) {
        return false;
      }
    }
    return true;  
  }

  let range = [] 

  for (let i = 2; i <= num; i++) {
    if (isPrime(i)) {
      range.push(i)
    }
  }

  return range.reduce((a, b) =>  a + b)









}
console.log(sumOfAllPrimes(10)); // will display 17 because 2 + 5 + 10 = 17



// Recommended (reduce with default value)
// Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.

// console.log(
//   [1, 2, 3, 4].reduce((a, b) => a + b, 0)
// )
// console.log(
//   [].reduce((a, b) => a + b, 0)
// )

//  a prime number can only return false - your code --> "if num % x ===0 { return false}" if it % 1 OR ITSELF. But you put x start from 2, so it won't check 1. then u put "x < num"  so it won't check itself
Enter fullscreen mode Exit fullscreen mode

Top comments (0)