Javascript Problem Solving

Javascript Problem Solving

In this blog, we are going to explore - how to solve problems using Javascript

·

4 min read

What is Problem Solving?

Problem-solving in programming is identifying a solution and steps / Techniques to solve the problem.

In this blog, we will solve five fundamental problems using Javascript.

Problem 1: Find a Factorial of a number

Problem Algorithm:

We need to find the factorial of the number, for example, let’s find the factorial for the number 5! = 5 x 4 x 3 x 2 x 1 = 120. Therefore, the factorial for the number 5 is 120.

Step 1: Start the Program.

Step 2: Declare the variable(num) to store the input value for which factorial needs to be found.

Step 3: Declare another variable(factorial) to store the factorial output value.

Step 4: now store the num value in the factorial variable at first.

Step 5: Give iteration using a while loop with condition (i.e., num value is greater than 1).

Step 6: if the num value is greater than 1 it will come into the while loop.

Step 7: num value is decremented.

Step 8: Multiply factorial and num values and store them in the factorial variable.

Step 9: Repeat step 6 and step 7, until the condition gets failed(i.e., num value is greater than 1).

Step 10: when the num value is equal to 1, the condition gets failed and it will come out of the loop and returns the output, which is the factorial value.

Step 11: End the program.

Program Code:

var num=5;
factorial=num;
while(num>1){
    num--;
    factorial=factorial*num;
}
console.log(factorial);  //120

Problem 2: Fibonacci Series of first 10 numbers

Problem Algorithm:

Step 1: Start the Program.

Step 2: Declare the num variable and it holds the series of values till which we going to generate the Fibonacci series.

Step 3: Declare two variables named firstNo and secondNo and store 0 in firstNo and 1 in secondNo.

Step 4: for loop is used to iterate through the loop and generate the Fibonacci series and it is given with the condition to check if i is less than the num value.

Step 5: if the condition is satisfied it will execute the statements inside the for loop.

Step 6: print the firstNo value.

Step 7: add firstNo and secondNo values and store them in the thirdNo variable.

Step 8: swap the firstNo with the secondNo.

Step 9: swap the secondNo with the thirdNo.

Step 10: Repeat Step 6 to Step 9, until the condition gets fails.

Step 11: End the program.

Problem Code:

var num=10;
firstNo=0;
secondNo=1;
for(i=0;i<num;i++){
    console.log(firstNo);
    thirdNo=firstNo + secondNo;
    firstNo=secondNo;
    secondNo=thirdNo;
} 
Output
0
1
1
2
3
5
8
13
21
34

Problem 3: Reverse a string

In Javascript, to reverse a string we have a method called reverse()

Problem Algorithm:

Step 1: Start the program.

Step 2: declare the variable str and store the string which needs to be reversed.

Step 3: Using the split() method we will split the string into an array of characters and store it in the variable named array.

Step 4: Next, step we will use the reverse() method to reverse the array elements which are stored in the array variable.

Step 5: We will convert array elements into string characters using the join() method and print the output.

Step 6: End the Program.

Problem Code:

var str='hello programmers';
var array=str.split('');
var rev_arr=array.reverse();
var join_str=rev_arr.join('');
console.log(join_str); // sremmargorp olleh

Problem 4: Return the number of vowels in a string

Problem Algorithm:

Step 1: Start the Program.

Step 2: declare the variable str to store the string characters.

Step 3: next declare the count variable to store the count of vowels in the string after performing the match() method and the length property and print the output.

Step 4: End the program.

Problem Code:

var str='hello programmers';
var count=str.match(/[aeiou]/gi).length;
console.log(count);

Here, we are using the match() method. Match() method is used to match the characters given inside it and the length property will calculate the length of characters it matched.

Problem 5: sum of numbers within an array

Problem Algorithm:

Step 1: Start the program.

Step 2: Declare a variable named arr to store array elements.

Step 3: Declare a variable sum with the value of 0.

Step 4: for loop is used to iterate through the array of elements until the condition gets failed and print the output.

Step 5: End the Program.

Problem Code:

var arr=[1,2,3,4,5];
var sum=0;
for(i=0;i<arr.length;i++){
    var sum = sum+arr[i];
}
console.log(sum); //15

In the next blog, we will explore arrays in detail.