Skip to main content

Basics Problems

These problems cover the fundamentals: variables, conditionals, loops, functions, and strings. Try to solve each one on your own before opening the solution. Open your browser console (press F12) and code along!

1. Ping-Pong (FizzBuzz Variant)

Print the numbers from 1 to 20. For multiples of 3, print Ping instead of the number. For multiples of 5, print Pong. For multiples of both, print PingPong.

Expected Output (first 15 lines)
1
2
Ping
4
Pong
Ping
7
8
Ping
Pong
11
Ping
13
14
PingPong
Solution
for (let i = 1; i <= 20; i++) {
let output = "";
if (i % 3 === 0) output += "Ping";
if (i % 5 === 0) output += "Pong";
console.log(output || i);
}

Instead of checking three separate conditions, we build the output string piece by piece. If the number is a multiple of both 3 and 5, both if blocks run and the string becomes PingPong for free. When the string stays empty, output || i falls back to the number itself.

2. Palindrome Check

Write a function isPalindrome(str) that returns true if a string reads the same forwards and backwards, ignoring case and spaces.

Example
isPalindrome("level"); // true
isPalindrome("Was it a car or a cat I saw"); // true
isPalindrome("Rizwan"); // false
Solution
const isPalindrome = (str) => {
const cleaned = str.toLowerCase().replaceAll(" ", "");
let left = 0;
let right = cleaned.length - 1;
while (left < right) {
if (cleaned[left] !== cleaned[right]) return false;
left++;
right--;
}
return true;
};

We normalize the string first (lowercase, no spaces), then use two pointers moving toward the middle. As soon as a pair of characters does not match, we return early. Time complexity is O(n) with O(1) extra comparisons — no reversed copy of the string is needed.

3. Vowel Counter

Write a function countVowels(str) that returns how many vowels (a, e, i, o, u) a string contains, regardless of case.

Example
countVowels("Ibrahim Ashiq"); // 5
countVowels("JS"); // 0
Solution
const countVowels = (str) => {
let count = 0;
for (const char of str.toLowerCase()) {
if ("aeiou".includes(char)) count++;
}
return count;
};

for...of walks the string character by character, and "aeiou".includes(char) is a compact membership check. In "Ibrahim Ashiq" the vowels are i, a, i, a, i — five in total.

4. Grade Calculator

Hafsa's school converts marks to letter grades: 90 and above is A, 80 to 89 is B, 70 to 79 is C, 60 to 69 is D, and anything below 60 is F. Write getGrade(marks).

Example
getGrade(93); // "A"
getGrade(87); // "B"
getGrade(58); // "F"
Solution
const getGrade = (marks) => {
if (marks >= 90) return "A";
if (marks >= 80) return "B";
if (marks >= 70) return "C";
if (marks >= 60) return "D";
return "F";
};

Because each if returns immediately, the conditions naturally act as ranges — by the time we check marks >= 80, we already know the marks are below 90. No else chains needed.

5. Multiplication Table

Write a function printTable(n) that prints the multiplication table of n from 1 to 10.

Expected Output for printTable(7)
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Solution
const printTable = (n) => {
for (let i = 1; i <= 10; i++) {
console.log(`${n} x ${i} = ${n * i}`);
}
};

printTable(7);

A single loop with a template literal keeps the formatting readable. Template literals are much cleaner than string concatenation with + when mixing text and values.

6. Reverse a String Without .reverse()

Write reverseString(str) without using the built-in Array.prototype.reverse() method.

Example
reverseString("Zakariya"); // "ayirakaZ"
reverseString("JavaScript"); // "tpircSavaJ"
Solution
const reverseString = (str) => {
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
};

We walk the string from the last index down to 0 and append each character to a new string. Time complexity is O(n). A recursive version also works, but the simple backwards loop is the clearest and fastest approach here.

7. Sum of Digits

Write sumDigits(num) that returns the sum of the digits of a positive integer — without converting the number to a string.

Example
sumDigits(4721); // 14
sumDigits(999); // 27
Solution
const sumDigits = (num) => {
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
return sum;
};

num % 10 extracts the last digit, and Math.floor(num / 10) drops it. For 4721 the loop picks up 1, 2, 7, 4 and returns 14. This runs in O(d) where d is the number of digits.

8. Find the Largest Number

Write findMax(numbers) that returns the largest number in an array — without using Math.max().

Example
findMax([23, 89, 4, 56]); // 89
findMax([-10, -3, -25]); // -3
Solution
const findMax = (numbers) => {
let max = numbers[0];
for (const num of numbers) {
if (num > max) max = num;
}
return max;
};

Start with the first element as the current maximum, then challenge it with every other value. Starting with numbers[0] (not 0) makes it work for arrays of negative numbers too. Single pass, O(n).

9. Character Counter

Write countChar(str, char) that returns how many times a character appears in a string.

Example
countChar("Hafsa", "a"); // 2
countChar("mississippi", "s"); // 4
Solution
const countChar = (str, char) => {
let count = 0;
for (const c of str) {
if (c === char) count++;
}
return count;
};

A straightforward single pass. As a one-liner alternative: str.split(char).length - 1 — splitting "Hafsa" on "a" produces 3 pieces, so there were 2 separators.

10. Star Triangle

Write printTriangle(rows) that prints a left-aligned triangle of stars.

Expected Output for printTriangle(4)
*
**
***
****
Solution
const printTriangle = (rows) => {
for (let i = 1; i <= rows; i++) {
console.log("*".repeat(i));
}
};

printTriangle(4);

String.prototype.repeat() replaces the classic inner loop — row i is simply the star repeated i times. If you want the nested-loop practice, build each row with an inner loop that appends one star at a time.