Skip to content
Laviyx Learn

Try

JavaScript Basics Practice

Practise JavaScript variables, numbers, strings, conditions and loops with small programs. Every solution has been run with Node.js and shows its real output.

Beginner7 problemsUpdated

Review first: Variables: let, const and var, Numbers and Math, Strings and Template Literals, if, else and Ternary, Loops: for, while, do...while

  1. 1.Split the bill

    Beginner

    A dinner costs ₹1,850 and 4 friends split it equally. Print each share with exactly two decimal places, like Each person pays ₹462.50.

    Show hint

    Divide, then format with toFixed(2) inside a template literal.

    Show solution
    JavaScript
    const total = 1850;
    const people = 4;
    console.log(`Each person pays ₹${(total / people).toFixed(2)}`);
    Output
    Each person pays ₹462.50
  2. 2.Minutes to hours and minutes

    Beginner

    A film lasts 135 minutes. Print it as 2 h 15 min.

    Show hint

    Math.floor(minutes / 60) gives the hours; % gives the remaining minutes.

    Show solution
    JavaScript
    const minutes = 135;
    const hours = Math.floor(minutes / 60);
    console.log(`${hours} h ${minutes % 60} min`);
    Output
    2 h 15 min
  3. 3.Initials from a full name

    Beginner

    Given const fullName = " asha devi rao ", print the initials in capitals with full stops: A.D.R.

    Show hint

    trim(), then split(" "), take the first character of each word and toUpperCase().

    Show solution
    JavaScript
    const fullName = "  asha devi rao ";
    const initials = fullName
      .trim()
      .split(" ")
      .map((word) => word[0].toUpperCase() + ".")
      .join("");
    console.log(initials);
    Output
    A.D.R.
  4. 4.Grade from marks

    Beginner

    Write a function grade(marks) that returns "A" for 90 and above, "B" for 75 to 89, "C" for 50 to 74 and "Fail" below 50. Print the grades for 95, 75, 60 and 42.

    Show hint

    Check the highest range first with if and return.

    Show solution
    JavaScript
    function grade(marks) {
      if (marks >= 90) return "A";
      if (marks >= 75) return "B";
      if (marks >= 50) return "C";
      return "Fail";
    }
    console.log([95, 75, 60, 42].map(grade).join(" "));
    Output
    A B C Fail
  5. 5.FizzBuzz

    Beginner

    Print the numbers from 1 to 15 on one line, but print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both.

    Show hint

    Check % 15 first, then % 3 and % 5. Collect the results in an array and join(" ").

    Show solution
    JavaScript
    const out = [];
    for (let n = 1; n <= 15; n++) {
      if (n % 15 === 0) out.push("FizzBuzz");
      else if (n % 3 === 0) out.push("Fizz");
      else if (n % 5 === 0) out.push("Buzz");
      else out.push(n);
    }
    console.log(out.join(" "));
    Output
    1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
  6. 6.Sum of digits

    Beginner

    Print the sum of the digits of 49152 using a while loop with % and division (no strings).

    Show hint

    n % 10 is the last digit; Math.floor(n / 10) removes it.

    Show solution
    JavaScript
    let n = 49152;
    let sum = 0;
    while (n > 0) {
      sum += n % 10;
      n = Math.floor(n / 10);
    }
    console.log(sum);
    Output
    21
  7. 7.Count vowels

    Beginner

    Count the vowels (a, e, i, o, u, in any case) in "JavaScript Is Fun" using a for...of loop.

    Show hint

    "aeiou".includes(ch.toLowerCase()) checks one character.

    Show solution
    JavaScript
    let count = 0;
    for (const ch of "JavaScript Is Fun") {
      if ("aeiou".includes(ch.toLowerCase())) count++;
    }
    console.log(count);
    Output
    5