5 beginner level JavaScript challenges to boost your skills

• Post author:

1. Write a function that takes in a string and returns the string with all vowels removed. For example, the input “javascript” should return “jvscrpt”.

To remove all vowels from a string, you can use the `replace` method to search for and replace any vowels in the string with an empty string. You can use a regular expression to match any vowels, using the `[aeiouAEIOU]` pattern. For example:

``````
function removeVowels(str) {
return str.replace(/[aeiouAEIOU]/g, '');
}
``````

2. Write a function that takes in an array of numbers and returns the sum of all the numbers in the array. For example, the input [1, 2, 3] should return 6.

To find the sum of all the numbers in an array, you can use a `for` loop to iterate through the array and add each number to a running total. For example

``````
function sumArray(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total += numbers[i];
}
}
``````

3. Write a function that takes in a number and returns “Fizz” if the number is divisible by 3, “Buzz” if it is divisible by 5, and “FizzBuzz” if it is divisible by both 3 and 5. For example, the input 15 should return “FizzBuzz”.

To return “Fizz”, “Buzz”, or “FizzBuzz” based on a number’s divisibility, you can use the `%` operator to check for the remainder when the number is divided by 3 or 5. If the number is divisible by 3, you can return “Fizz”. If it’s divisible by 5, you can return “Buzz”. If it’s divisible by both 3 and 5, you can return “FizzBuzz”. For example:

``````
function fizzBuzz(num) {
if (num % 3 === 0 && num % 5 === 0) {
return "FizzBuzz";
} else if (num % 3 === 0) {
return "Fizz";
} else if (num % 5 === 0) {
return "Buzz";
}
}
``````

4. Write a function that takes in a string and returns the string in reverse. For example, the input “javascript” should return “tpircsavaj”

To reverse a string, you can use the `split`, `reverse`, and `join` methods to first split the string into an array of characters, then reverse the array, and finally join the reversed array back into a string. For example:

``````
function reverseString(str) {
return str.split('').reverse().join('');
}
``````

5. Write a function that takes in an array of strings and returns an array with only the strings that have a length of 4 or more. For example, the input [“cat”, “dog”, “elephant”] should return [“elephant”].

To filter an array of strings to only include strings of a certain length, you can use the `filter` method and a callback function that checks the length of each string. The `filter` method will return a new array containing only the elements that return `true` when passed to the callback function. For example:

``````
function filterLongStrings(strings) {
return strings.filter(str => str.length >= 4);
}
``````

To conclude, practicing coding challenges is a great way for beginners to improve their JavaScript skills and gain confidence in their ability to solve problems. The five challenges listed above cover a range of concepts and techniques that are important to understand as a beginner, including string manipulation, array iteration, conditional statements, and function parameters. By working through these challenges, you can gain a solid foundation in JavaScript and continue to build on your skills as you progress in your coding journey.

Latest posts by Publisher (see all)

Publisher

Publisher @ideasorblogs