DEV Community

Sidra Maqbool
Sidra Maqbool

Posted on

6

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

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay