{ }codelper

50 JavaScript Practice Problems for Beginners

Here are 50 basic problems you solve with JavaScript syntax alone, with no web page involved. For each topic we picked an even spread, easiest first.

50 problemsLevel 1–2Solve in JavaScript

1. Input and output #1–5

Read all the input at once with require("fs").readFileSync(0, "utf8").
What you get is a string, so turn it into an array of numbers with .trim().split(/\s+/).map(Number).
Print with console.log.
For many lines, collect them in an array and print once with join("\n"), which is faster.

  1. Copying a guestbook line8P0311
  2. Sum of two numbers8P0001
  3. Bonus-added scores joined with commas8P0373
  4. Numbering the lines of a song8P0347
  5. Score statistics8P0003

2. Variables and types #6–10

Use const by default and let only for values that change.
"3" + 4 is not an error.
It gives "34", so always convert input with Number before you calculate.
Numbers are not split into integers and decimals, and they are exact only up to about 9 quadrillion (2^53).

  1. Marbles passed in turn8P0251
  2. The shorter one stands in front8P0344
  3. Four lines of true and false8P0217
  4. Ticket number from a seat code8P0226
  5. Sale price, or list price if there is none8P0281

3. Arithmetic #11–15

7 / 2 is 3.5.
When you need the quotient, use Math.floor or Math.trunc (the two give different results for negative numbers).
-7 % 2 is -1.
To print a fixed number of decimal places, use x.toFixed(2).

  1. Distance per liter8P0216
  2. Sum of digits8P0006
  3. How many combinations a luggage lock has8P0423
  4. Grid distance8P0017
  5. Grading addition homework8P0222

4. Conditionals #16–20

Compare with === and !==.
== converts types as it compares, so 0 == "" is true.
For a short choice, write condition ? a : b.

  1. A photo fitted to a square frame8P0254
  2. Letter grades8P0011
  3. Leap year8P0019
  4. Locker extra fee8P0372
  5. Three-day temperature trend8P0227

5. Loops #21–25

To repeat a set number of times, use for (let i = 0; i < n; i++).
To look at each value in an array, use for (const x of arr).
for...in gives you the indexes (as strings), not the values, so it is better not to use it on arrays.

  1. Clap game8P0002
  2. Star triangle8P0007
  3. One row of the times table8P0022
  4. One more throw for yut or mo8P0255
  5. First multiple8P0023

6. Strings #26–30

You cannot change a string, so you have to build a new one.
Reverse with [...s].reverse().join(""), cut with slice, and pad to a width with padStart.
To put a value inside a string, use backticks, as in `Hello, ${name}`.

  1. Count the vowels8P0009
  2. First and last position of a character8P0053
  3. Letters picked along the diagonal8P0283
  4. Count the words8P0012
  5. Palindrome check8P0004

7. Arrays #31–35

Without a comparison function, arr.sort() sorts the values as strings, so [10, 9, 1] becomes [1, 10, 9].
Sort numbers with arr.sort((a, b) => a - b).
sort and reverse change the original array, so if you still need it, copy it first with [...arr].

  1. Largest number and its position8P0005
  2. Numbers of the students who retake8P0374
  3. Showing one page of a board8P0284
  4. Sort the numbers8P0036
  5. Sorting sign letters8P0253

8. Sum, average, maximum #36–40

Get the sum with arr.reduce((a, b) => a + b, 0) and the maximum with Math.max(...arr).
If you leave out the starting value 0 in reduce, an empty array causes an error.

  1. Dart scores thrown in turns8P0342
  2. Mornings at or above average8P0343
  3. Second largest8P0031
  4. Class averages and the year average8P0225
  5. The day closest to the average8P0418

9. Functions #41–45

Make one with function, or write an arrow function like const f = (x) => x * 2.
If you use curly braces in an arrow function, you have to write return yourself.
Leave it out and you get undefined back.

  1. A thermometer that cannot leave its scale8P0402
  2. Balls that landed in the sandbox8P0285
  3. Prime check8P0008
  4. A running course made of three flags8P0435
  5. A box-count function with defaults8P0257

10. Objects and Map #46–50

To look up a value by name, use an object {} or a Map.
The basic pattern for counting is count[k] = (count[k] ?? 0) + 1.
When the keys are numbers or the insertion order matters, Map is the safe choice.

  1. Tally scans in order8P0220
  2. Phone book lookup8P0063
  3. Only the last vote counts8P0228
  4. Find mutual picks8P0256
  5. Clearing out the fridge8P0410

Good to know

See also