JavaScript exercises with solutions
Solve in editor
Write your solution right in the browser and have it checked automatically.
Open exercises with editor1. Even numbers Beginner
Print all even numbers from 1 to 10 – each on its own line.
Expected output: 2 ⏎ 4 ⏎ 6 ⏎ 8 ⏎ 10
Note
Count in steps of two (i += 2) or check with i % 2 === 0.
Show sample solution
for (let i = 2; i <= 10; i += 2) {
console.log(i);
}2. FizzBuzz (1–15) Beginner
Print the numbers 1 to 15. For multiples of 3 print Fizz instead, for multiples of 5 print Buzz, and for both print FizzBuzz.
Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz
Note
Check the "divisible by 15" case first.
Show sample solution
for (let i = 1; i <= 15; i++) {
if (i % 15 === 0) console.log("FizzBuzz");
else if (i % 3 === 0) console.log("Fizz");
else if (i % 5 === 0) console.log("Buzz");
else console.log(i);
}3. Reverse a word Beginner
Reverse the string "JavaScript" and print it.
Expected output: tpircSavaJ
Note
Strings have no reverse – split them into an array with [...wort], reverse it and join it back together with join("").
Show sample solution
const wort = "JavaScript";
console.log([...wort].reverse().join(""));4. Sum of squares Beginner
Calculate the sum of the squares of the numbers 1 to 10 and print it.
Expected output: 385
Note
A loop with a running total is enough. Or Array.from({ length: 10 }, ...) with reduce.
Show sample solution
let summe = 0;
for (let i = 1; i <= 10; i++) {
summe += i ** 2;
}
console.log(summe);5. Palindrome function Intermediate
Write a function istPalindrom(text) that returns true if the text reads the same forwards and backwards (ignoring case). Print istPalindrom("Anna") and istPalindrom("JavaScript").
Expected output: true ⏎ false
Note
First convert with toLowerCase() and compare with the reversed version ([...t].reverse().join("")).
Show sample solution
function istPalindrom(text) {
const t = text.toLowerCase();
return t === [...t].reverse().join("");
}
console.log(istPalindrom("Anna"));
console.log(istPalindrom("JavaScript"));6. Most frequent word Intermediate
Find the most frequent word in "a b a c b a" and print it.
Expected output: a
Note
Count with an object or a Map, then look for the entry with the largest value (Object.entries(...).sort(...)).
Show sample solution
const text = "a b a c b a";
const zaehler = {};
for (const wort of text.split(" ")) {
zaehler[wort] = (zaehler[wort] ?? 0) + 1;
}
const [haeufigstes] = Object.entries(zaehler).sort((x, y) => y[1] - x[1])[0];
console.log(haeufigstes);7. Primes up to 30 Intermediate
Print all prime numbers up to 30 on one line, separated by spaces.
Expected output: 2 3 5 7 11 13 17 19 23 29
Note
A number n is prime if it is not divisible by any number from 2 to n-1. Collect the matches in an array and print it with join(" ").
Show sample solution
const primzahlen = [];
for (let n = 2; n <= 30; n++) {
let prim = true;
for (let d = 2; d < n; d++) {
if (n % d === 0) {
prim = false;
break;
}
}
if (prim) primzahlen.push(n);
}
console.log(primzahlen.join(" "));8. Counter factory (closure) Advanced
Write a function erzeugeZaehler(start) that returns a function. Each call of that function increases the counter by 1 and returns the new value. Every counter created has its own state.
Expected output: 11 ⏎ 12 ⏎ 1
Note
Create a local variable in erzeugeZaehler and return an arrow function that modifies that variable (closure).
Show sample solution
function erzeugeZaehler(start) {
let n = start;
return () => ++n;
}
const a = erzeugeZaehler(10);
const b = erzeugeZaehler(0);
console.log(a());
console.log(a());
console.log(b());