GithubHelp home page GithubHelp logo

beginner-friendly-problems-js's Introduction

beginner-friendly-problems-js

Certainly! Here are 10 beginner-level programming problems involving for loops and if...else statements in JavaScript. Each problem comes with a brief description and a solution.

Problem 1: Sum of Numbers

Write a program that calculates the sum of all numbers from 1 to 10 using a for loop.

Solution:

let sum =  0;
for (let i =  1; i <=  10; i++) {
    sum += i;
}
console.log(sum); // Output:  55

Problem 2: Even Numbers

Write a program that prints out all even numbers between 1 and 20 using a for loop and an if statement.

Solution:

for (let i =  1; i <=  20; i++) {
    if (i %  2 ===  0) {
        console.log(i);
    }
}

Problem 3: Odd Numbers

Similar to Problem 2, write a program that prints out all odd numbers between 1 and 20 using a for loop and an if statement.

Solution:

for (let i =  1; i <=  20; i++) {
    if (i %  2 !==  0) {
        console.log(i);
    }
}

Problem 4: Factorial

Write a program that calculates the factorial of a number using a for loop. The factorial of a number n is the product of all positive integers less than or equal to n.

Solution:

const n = 5;
let result =  1;
for (let i =  1; i <= n; i++) {
    result *= i;
}

Problem 5: Check Prime Number

Write a program that checks if a number is prime using a for loop and an if statement.

Solution:

let num =   7;
let isPrime = true;
if (num <=   1) {
    isPrime = false;
} else {
    for (let i =   2; i < num; i++) {
        if (num % i ===   0) {
            isPrime = false;
            break;
        }
    }
}
console.log(isPrime); // Output: true

Problem 6: Find the smallest Number

Write a program that will find the smalles number among three numbers.

Solution

let num1 =  5;
let num2 =  3;
let num3 =  7;

let smallest;

if (num1 < num2 && num1 < num3) {
    smallest = num1;
} else if (num2 < num1 && num2 < num3) {
    smallest = num2;
} else {
    smallest = num3;
}

console.log(smallest); // Output will be the smallest number among num1, num2, and num3

Problem 7: Find the sum of the odd numbers between 1 - 20

Write a program that will find the sum of the odd numbers between 1 - 20

Solution

let sum = 0;
for(let i = 1; i <= 20; i++){
if(i%2 !== 0){
sum+=i;
}
}
console.log(sum)

beginner-friendly-problems-js's People

Contributors

mahfuzswaron avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.