top of page
Search

A Beginner's Guide to Solving Five Key JavaScript Challenges

  • Writer: Marko Reljić
    Marko Reljić
  • Jul 10, 2023
  • 3 min read

Starting your journey into programming, especially as a beginner developer or intern, can sometimes feel overwhelming with the multitude of concepts and syntax to grasp. It's often through hands-on problem-solving that these concepts begin to sink in. Today, we're going to navigate together through five JavaScript coding problems. Each of these exercises is specifically chosen to reflect fundamental aspects of JavaScript, and they'll allow us to explore some of the key concepts of the language. Ready to dive in? Let's get started!





1. Printing All Even Numbers from 0 to 10:

As our first task, we'll create a function named printEvenNumbers to print all even numbers between 0 and 10. This problem introduces us to two fundamental concepts: loops and conditional logic.

function printEvenNumbers() {
  for (let i = 0; i <= 10; i += 2) {
    console.log(i);
  }
}
printEvenNumbers();

In this function, we've used a for loop, which allows us to repeat a block of code for a specified number of iterations. Here, we're starting from 0 (let i = 0), ending at 10 (i <= 10), and incrementing by 2 each time (i += 2). This increment of 2 ensures that we're only printing out even numbers, as it effectively skips the odd numbers in our range. The console.log(i) within the loop prints the current number.

2. Length Converter Function:

Our next challenge is to create a length converter function named lengthConverter that converts kilometers into miles. This problem gives us a chance to practice writing functions - reusable blocks of code that perform specific tasks.

function lengthConverter(kilometers) {
  const miles = kilometers * 0.621371;
  return miles;
}

console.log(lengthConverter(10));

This function takes one parameter - kilometers - which represents the distance in kilometers that we want to convert. Within the function, we create a constant miles that calculates the equivalent distance in miles using the conversion factor 0.621371. Finally, we return this value, effectively giving it as the output of our function when it's called. The console.log(lengthConverter(10)); line is how we call our function, passing in 10 as the distance to convert. 3. Reversing an Array:

Our third challenge involves reversing an array - a common operation when manipulating data. While JavaScript offers built-in functions for this purpose, it's helpful for beginners to understand how such operations work at a fundamental level.

function reverseArray(arr) {
  const newArr = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    newArr.push(arr[i]);
  }
  return newArr;
}

console.log(reverseArray([1, 2, 3, 4, 5]));

Here, we're taking in an array arr and creating a new empty array newArr. Then, we set up a for loop, but this time we're starting from the end of arr (arr.length - 1) and moving towards the beginning, decrementing our counter i at each step (i--). In each loop iteration, we're using newArr.push(arr[i]) to add the current element from arr to newArr, effectively reversing the array.

4. Removing Negative Numbers from an Array:


Let's explore how to remove all negative numbers from an array using a simple for loop and conditional logic in JavaScript.


Here's the code:

function removeNegativeNumbers(arr) {
  const result = [];
  for(let i = 0; i < arr.length; i++) {
    if(arr[i] >= 0) {
      result.push(arr[i]);
    }
  }
  return result;
}

console.log(removeNegativeNumbers([-1, 2, -3, 4, -5]));

We declare a function removeNegativeNumbers taking arr as an argument.

Inside the function, we create an empty array result to store non-negative numbers.

We loop through arr. If a number is not negative (greater than or equal to 0), we add it to result. After checking all numbers, we return result which contains all non-negative numbers from arr. This function exemplifies the use of basic concepts like loops and conditionals to manipulate arrays in JavaScript. 5. Counting Vowels in a String:

Our final problem involves writing a function countVowelsInString that counts the number of vowels in a string. This allows us to practice working with strings and regular expressions.

function countVowelsInString(string) {
  return string.match(/[aeiou]/gi).length;
}

console.log(countVowelsInString("hello world"));

We use the match function with a regular expression to find all matches of the vowels (a, e, i, o, u) in the string. The g in the regular expression stands for 'global', which means we're looking through the whole string, and i stands for 'case-insensitive', which allows us to match both lower and uppercase vowels. match returns an array of all the matches, and we use .length to count the number of matches (i.e., the number of vowels). By tackling these five challenges, we've covered a range of fundamental JavaScript concepts, including loops, functions, array and string manipulation, and more. Each problem might appear simple on the surface, but they all carry valuable lessons for beginners on how JavaScript works and how to think about problem-solving in programming. Keep practicing, keep experimenting, and keep coding! You're on your way to becoming a proficient developer!

 
 
 

Comments


bottom of page