Skip to content

JavaScript function exercises

  • by

JavaScript function is a block of code used to perform specific tasks. You have to do JavaScript function exercises for exams and learning purposes.

JavaScript function exercises for beginners examples

Simple HTML example code of JavaScript function:

1. Write a function that takes two numbers as arguments and returns their sum.

function add(a, b) {
  return a + b;
}
console.log(add(1,2))

2. Write a function that takes an array of numbers as an argument and returns the sum of all the numbers.

 function sum(numbers) {
  let total = 0;
  for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  return total;
}
console.log(sum([1,2,3]))

3. Write a function that takes an array of strings as an argument and returns the number of strings that start with the letter “a”.

 function countStringsStartingWithA(strings) {
  let count = 0;
  for (let i = 0; i < strings.length; i++) {
    if (strings[i][0] === "a") {
      count++;
    }
  }
  return count;
}
console.log(countStringsStartingWithA(['a','b','ab']))

4. Write a function that takes a number as an argument and returns “even” if the number is even, and “odd” if the number is odd.

function evenOrOdd(number) {
  return number % 2 === 0 ? "even" : "odd";
}

5. Write a function that takes an array of numbers as an argument and returns the largest number in the array.

function maxNumber(numbers) {
  let max = numbers[0];
  for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] > max) {
      max = numbers[i];
    }
  }
  return max;
}

6. Write a function that takes a string as an argument and returns the string reversed.

function reverseString(str) {
  let reversed = "";
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return reversed;
}

7. Write a function that takes a number as an argument and returns the factorial of that number.

function factorial(number) {
  if (number === 0) {
    return 1;
  } else {
    return number * factorial(number - 1);
  }
}

8. Write a function that takes two numbers as arguments and returns the smaller number.

function minNumber(a, b) {
  return a < b ? a : b;
}

9. Write a JavaScript function to print the “Hello World” message.

function displayMessage() {
  console.log("Hello World");
}

displayMessage(); //"Hello World"

10. Write a JavaScript function that generates all combinations of a string.

<!DOCTYPE html>
<html>
 <body>

<script>
function generateCombinations(str) {
  const combinations = [];

  function generate(current, remaining) {
    if (remaining.length === 0) {
      combinations.push(current);
    } else {
      generate(current + remaining[0], remaining.slice(1));
      generate(current, remaining.slice(1));
    }
  }

  generate("", str);
  return combinations;
}

const str = "abc";
const combinations = generateCombinations(str);
console.log(combinations);
</script>

</body>
</html>

Output:

JavaScript function exercises

Do comment if you have any doubts or suggestions on this Js function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *