10 JavaScript Function Code Challenges for Beginners

sidramaqbool

Sidra Maqbool

Posted on February 23, 2023

10 JavaScript Function Code Challenges for Beginners

Functions are an essential part of JavaScript programming. They allow us to write reusable blocks of code that can take inputs and produce outputs. In this post, we'll go through 10 function challenges that are perfect for beginners. We'll provide a brief description of each challenge, along with an example function that solves the challenge. Each example function will be available on GitHub, so you can see how it works and experiment with it yourself.

1. Greet
Write a function called greet that takes in a name and outputs "Hello, {name}!".

function greet(name) {
  return `Hello, ${name}!`;
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

2. Sum
Write a function called sum that takes in two numbers and outputs their sum.

function sum(num1, num2) {
  return num1 + num2;
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

3. Multiply
Write a function called multiply that takes in two numbers and outputs their product.

function multiply(num1, num2) {
  return num1 * num2;
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

4. Is Even
Write a function called isEven that takes in a number and outputs true if it's even and false otherwise.

function isEven(num) {
  return num % 2 === 0;
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

5. Reverse String
Write a function called reverseString that takes in a string and outputs the reversed version of it.

function reverseString(str) {
  return str.split('').reverse().join('');
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

6. Count Chars
Write a function called countChars that takes in a string and a character and outputs the number of times that character appears in the string.

function countChars(str, char) {
  return str.split('').filter(c => c === char).length;
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

7. Calculate Area
Write a function called calculateArea that takes in the length and width of a rectangle and outputs its area.

function calculateArea(length, width) {
  return length * width;
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

8. Capitalize
Write a function called capitalize that takes in a string and outputs the same string with the first letter capitalized.

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

9. Filter Evens
Write a function called filterEvens that takes in an array of numbers and outputs a new array containing only the even numbers.

function filterEvens(arr) {
  return arr.filter(num => num % 2 === 0);
}
Enter fullscreen mode Exit fullscreen mode

Working example on GitHub

10. Calculate Discount
Write a function called calculateDiscount that takes in a price and a discount percentage and outputs the discounted price.

Working example on GitHub

Thanks for reading! I hope you found this post informative and helpful. If you have any questions or feedback, please feel free to leave a comment below

💖 💪 🙅 🚩
sidramaqbool
Sidra Maqbool

Posted on February 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related