Ad
Fundamentals

Write a function sumOfPrimes(n) that takes an integer n and returns the sum of all prime numbers less than or equal to n.

function isPrime(num) {
    if (num <= 1) return false;
    for (let i = 2; i <= Math.sqrt(num); i++) {
        if (num % i === 0) return false;
    }
    return true;
}

function sumOfPrimes(n) {
    let sum = 0;
    for (let i = 2; i <= n; i++) {
        if (isPrime(i)) {
            sum += i;
        }
    }
    return sum;
}
Code
Diff
  • /*def hello_world(world):
        if world == True:
            return "Hello World baby"
        elif world == False:
            return "No World"*/
    
    // helloWorld = (world) => world == true ? 'Hello World baby': 'No World';
    
    const helloWorld= world => world ? `Hello World baby`:`No World`;
    • /*def hello_world(world):
    • if world == True:
    • return "Hello World baby"
    • elif world == False:
    • return "No World"*/
    • // helloWorld = (world) => world == true ? 'Hello World baby': 'No World';
    • const helloWorld=(world)=>world==true?`Hello World baby`:`No World`;
    • const helloWorld= world => world ? `Hello World baby`:`No World`;