JavaScript Logic Building Practice Questions with Solutions

Introductions

Logic building is one of the most important skills for becoming a good JavaScript programmer. Knowing syntax is not enough—you should also be able to think through a problem, break it into smaller steps, and write the correct solution.

In this chapter, you will practice numbers, strings, loops, arrays, conditions, patterns, counting, searching, and problem-solving logic with simple JavaScript examples. JavaScript Logic building practice Questions help to build concepts.

Question 1: Check Whether a Number Is Even or Odd

Problem

Write a JavaScript program that checks whether a number is even or odd.

Solution

const number = 15;

if (number % 2 === 0) {
    console.log("Even");
} else {
    console.log("Odd");
}

Output

Odd

Step-by-step Explanation

The % operator gives the remainder.

For example:

15 % 2 = 1

Because the remainder is not 0, the number is odd.

For an even number:

16 % 2 = 0

So the condition:

number % 2 === 0

checks whether the number is evenly divisible by 2.


Question 2: Find the Largest of Three Numbers

Problem

Find the largest number among three numbers.

Solution

const a = 25;
const b = 40;
const c = 30;

let largest;

if (a >= b && a >= c) {
    largest = a;
} else if (b >= a && b >= c) {
    largest = b;
} else {
    largest = c;
}

console.log("Largest:", largest);

Output

Largest: 40

Step-by-step Explanation

First, compare a with both other numbers:

a >= b && a >= c

If that is false, compare b:

b >= a && b >= c

If both conditions are false, c must be the largest.

This teaches an important logic-building technique:

Compare one possibility, eliminate it if necessary, and continue checking.


Question 3: Find the Sum of Numbers from 1 to N

Problem

Find the sum of all numbers from 1 to 10.

Solution

const n = 10;

let sum = 0;

for (let i = 1; i <= n; i++) {
    sum += i;
}

console.log("Sum:", sum);

Output

Sum: 55

Step-by-step Explanation

Initially:

let sum = 0;

The loop starts at 1:

for (let i = 1; i <= n; i++)

The values are added one by one:

0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
...
45 + 10 = 55

Therefore:

Sum = 55

Question 4: Reverse a String

Problem

Reverse the string "JavaScript" without using a built-in reverse method.

Solution

const text = "JavaScript";

let reversed = "";

for (let i = text.length - 1; i >= 0; i--) {
    reversed += text[i];
}

console.log(reversed);

Output

tpircSavaJ

Step-by-step Explanation

The last character is at:

text.length - 1

The loop moves backwards:

for (let i = text.length - 1; i >= 0; i--)

Characters are added to reversed one by one.

For example:

t
tp
tpi
tpir
...

Finally:

tpircSavaJ

Question 5: Count Vowels in a String

Problem

Count how many vowels are present in the string "JavaScript".

Solution

const text = "JavaScript";

let count = 0;

for (let i = 0; i < text.length; i++) {

    const character = text[i].toLowerCase();

    if (
        character === "a" ||
        character === "e" ||
        character === "i" ||
        character === "o" ||
        character === "u"
    ) {
        count++;
    }
}

console.log("Vowels:", count);

Output

Vowels: 3

Step-by-step Explanation

The string contains:

J a v a S c r i p t

The vowels are:

a
a
i

Therefore, the answer is:

3

The count variable increases whenever a vowel is found:

count++;

This is a common counting logic pattern.


Question 6: Check Whether a String Is a Palindrome

Problem

Check whether "madam" reads the same forward and backward.

Solution

const text = "madam";

let reversed = "";

for (let i = text.length - 1; i >= 0; i--) {
    reversed += text[i];
}

if (text === reversed) {
    console.log("Palindrome");
} else {
    console.log("Not a palindrome");
}

Output

Palindrome

Step-by-step Explanation

The original string is:

madam

After reversing:

madam

Both strings are equal:

text === reversed

Therefore, it is a palindrome.

Other examples:

level → Palindrome
radar → Palindrome
hello → Not a palindrome

Question 7: Find the Largest Number in an Array

Problem

Find the largest number from an array without using Math.max().

Solution

const numbers = [12, 45, 7, 89, 23];

let largest = numbers[0];

for (let i = 1; i < numbers.length; i++) {

    if (numbers[i] > largest) {
        largest = numbers[i];
    }

}

console.log("Largest:", largest);

Output

Largest: 89

Step-by-step Explanation

Initially, assume the first number is the largest:

let largest = numbers[0];

So:

largest = 12

Then compare each number with largest.

When JavaScript finds:

89 > 45

the value of largest becomes:

89

At the end:

Largest = 89

This is a very important comparison logic pattern.


Question 8: Count the Occurrence of Each Element

Problem

Count how many times each number appears in an array.

Solution

const numbers = [1, 2, 2, 3, 1, 2, 3, 3];

const frequency = {};

for (const number of numbers) {

    if (frequency[number]) {
        frequency[number]++;
    } else {
        frequency[number] = 1;
    }

}

console.log(frequency);

Output

{
    1: 2,
    2: 3,
    3: 3
}

Step-by-step Explanation

The array is:

1, 2, 2, 3, 1, 2, 3, 3

The program keeps a count for every number.

For 1:

1 appears 2 times

For 2:

2 appears 3 times

For 3:

3 appears 3 times

The important logic is:

if (frequency[number]) {
    frequency[number]++;
} else {
    frequency[number] = 1;
}

If the number already exists, increase its count. Otherwise, start its count at 1.


Question 9: Find the Second Largest Number

Problem

Find the second largest number from the array:

[10, 25, 45, 30, 50]

Solution

const numbers = [10, 25, 45, 30, 50];

let largest = -Infinity;
let secondLargest = -Infinity;

for (const number of numbers) {

    if (number > largest) {
        secondLargest = largest;
        largest = number;
    } else if (
        number > secondLargest &&
        number !== largest
    ) {
        secondLargest = number;
    }

}

console.log("Second Largest:", secondLargest);

Output

Second Largest: 45

Step-by-step Explanation

Initially:

largest = -Infinity
secondLargest = -Infinity

When 50 becomes the largest number:

largest = 50
secondLargest = 45

The important idea is that when a new largest value is found, the old largest value becomes the second largest:

secondLargest = largest;
largest = number;

This is a useful technique for solving ranking problems.


Question 10: Find Missing Number from an Array

Problem

An array contains numbers from 1 to 5, but one number is missing. Find the missing number.

[1, 2, 3, 5]

Solution

const numbers = [1, 2, 3, 5];

const n = 5;

let expectedSum = 0;

for (let i = 1; i <= n; i++) {
    expectedSum += i;
}

let actualSum = 0;

for (const number of numbers) {
    actualSum += number;
}

const missingNumber = expectedSum - actualSum;

console.log("Missing Number:", missingNumber);

Output

Missing Number: 4

Step-by-step Explanation

First, calculate the expected sum:

1 + 2 + 3 + 4 + 5 = 15

The actual array contains:

1 + 2 + 3 + 5 = 11

Now subtract:

15 - 11 = 4

Therefore:

Missing Number = 4

This is an example of using mathematical logic to simplify a programming problem.

Key Takeaways

  • Logic building is about solving problems step by step.
  • Start by understanding exactly what the problem asks.
  • Break large problems into smaller steps.
  • Use variables to store intermediate results.
  • Use conditions to make decisions.
  • Use loops when a task needs repetition.
  • Use counters when you need to count something.
  • Use comparison logic to find maximum and minimum values.
  • Use strings and arrays carefully when solving problems.
  • Frequency counting is a common interview and programming technique.
  • Palindrome problems are useful for practicing string logic.
  • Reverse problems help develop loop and indexing skills.
  • Missing-number problems can often be simplified using mathematics.
  • Avoid trying to write the entire solution at once.
  • First create the logic on paper or in simple steps, then convert it into JavaScript.
  • Practice the same problem using different approaches to improve problem-solving skills.

FAQs

1. What is logic building in JavaScript?

Logic building means learning how to analyze a problem and create a sequence of steps that produces the correct result.

For example, to find the largest number:

1. Take the first number.
2. Compare it with the next number.
3. Keep the larger number.
4. Continue until the array ends.
5. Return the largest number.

Then convert those steps into JavaScript.

2. How can I improve my JavaScript logic?

Practice small problems regularly.

Start with:

  • Even and odd numbers
  • Positive and negative numbers
  • Largest and smallest values
  • String reversal
  • Palindromes
  • Counting
  • Array searching
  • Duplicate removal
  • Frequency counting
  • Number patterns

Gradually increase the difficulty.

3. Should I memorize JavaScript logic-building solutions?

No. Understanding the approach is more important than memorizing code.

For example, instead of memorizing a largest-number solution, understand the idea:

Assume the first value is largest.
Compare every other value.
Replace largest when a bigger value is found.

Once you understand that pattern, you can solve many similar problems.

4. What should I do before writing JavaScript code?

First identify:

  1. What is the input?
  2. What output is required?
  3. What conditions are involved?
  4. Is repetition required?
  5. Do I need a counter?
  6. Do I need an array, object, Map, or Set?
  7. Can the problem be divided into smaller steps?

Then start coding.

5. Why are loops important for logic building?

Loops allow you to repeat an operation without writing the same code again and again.

For example:

for (let i = 0; i < numbers.length; i++) {
    console.log(numbers[i]);
}

This allows you to process every element of an array.

6. Are logic-building questions useful for JavaScript interviews?

Yes. JavaScript interviews often test your ability to analyze and solve problems, not just remember syntax.

Common topics include:

  • Strings
  • Arrays
  • Objects
  • Loops
  • Searching
  • Sorting
  • Counting
  • Frequency
  • Recursion
  • Basic algorithms

7. How should a beginner practice JavaScript problems?

Start with easy problems and solve them without looking at the answer.

A good process is:

Understand the problem
        ↓
Write the steps
        ↓
Try the solution
        ↓
Test with different inputs
        ↓
Find mistakes
        ↓
Improve the solution

The goal is not to write complicated code. The goal is to develop clear programming thinking.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top