Project: JavaScript Toolkit
Instructions

Sum two numbers

Step 1 of 6
Return the sum of two numbers.
The simplest function, just to warm up.

Instructions

In script.js:

  • Find the addNumbers stub.
  • Return a + b.
  • Use return, not console.log.
Hints

One line: return a + b;.

Refresher: JS Functions.

How it works

Capitalize the first letter

Step 2 of 6
Return s with the first letter upper-cased.
Example: "kai" becomes "Kai".

Instructions

In script.js:

  • Capitalize the first character with s.charAt(0).toUpperCase().
  • Append the remaining characters with s.slice(1).
  • Return the two parts concatenated with +.
Hints

Edge case: if s is empty, return "" so the helper does not crash.

Strings have a .toUpperCase() method.

See JS String Methods.

How it works

Reverse a string

Step 3 of 6
Return s with its characters in reverse order.
Example: "hello" becomes "olleh".

Instructions

In script.js:

  • Start with let result = "".
  • Loop with for (let i = s.length - 1; i >= 0; i--).
  • Inside the loop, write result += s.charAt(i).
  • Return result.
Hints

The point is the for loop.

Do not use .split("").reverse().join("").

How it works

Check for a palindrome

Step 4 of 6
Return true if s reads the same forwards and backwards.
Case-insensitive.

Instructions

In script.js:

  • Lower-case s first with const lower = s.toLowerCase().
  • Compare it with its reverse by reusing reverseString.
  • Return the boolean result of the comparison.
Hints

Edge case: an empty string reads the same forwards and backwards, so it returns true naturally.

You can call your own helpers.

Example: return lower === reverseString(lower);.

How it works

Count the vowels

Step 5 of 6
Return how many vowels are in s.
Count both upper and lower case.

Instructions

In script.js:

  • Start with let count = 0.
  • Loop with for (let i = 0; i < s.length; i++).
  • For each character, check "aeiouAEIOU".indexOf(s.charAt(i)) !== -1.
  • If true, run count++.
  • Return count.
Hints

indexOf returns -1 when the character is not in the lookup string.

How it works

Render results

Step 6 of 6
Wire it all up.
Fill in init() so each function result shows up on the page.

Instructions

In script.js:

  • Inside init(), read each input constant at the top of the file.
  • Call each helper and write the result to its target with textContent.
  • addNumbers writes to #r-add.
  • capitalize writes to #r-cap.
  • reverseString writes to #r-rev.
  • isPalindrome writes to #r-pal.
  • countVowels writes to #r-vow.
Hints

Use document.getElementById("r-add").textContent = String(addNumbers(NUM_A, NUM_B));.

See DOM textContent.

How it works
Click Submit to grade your project.