Javascript Problem Solving(Part 2)

Javascript Problem Solving(Part 2)

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

Anusha V's photo
Anusha V
·Jul 31, 2022·

3 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

In Part - 1 we have solved five fundamental problems using Javascript. If you missed the Javascript Problem Solving Part - 1 blog, check it out here

In today’s blog, we will solve another three fundamental problems using Javascript.

Problem - 1: Filter the negative numbers in an array

Program Algorithm:

Step-1: Start the Program.

Step-2: Declare the array inside the variable name i.e., arr.

Step-3: we will use a Javascript in-built filter() function used to filter the array elements, which will filter the array of elements that satisfy the particular condition(i.e., it should display only the array elements which are greater than -1.).

Step-4: Inside the filter() function, we will give the condition as the call-back function and it will filter out the array elements.

Step-5: The resultant will also be in an array and it is stored in the res_arr variable.

Step-6: End the Program.

Program Code:

let arr = [1,-8,5,6,-4,-2,-1,0,7,9,3];
let res_arr = arr.filter(
    function result(a){
        return a >-1
    }
    )
console.log(res_arr); //[ 1, 5, 6, 0, 7, 9, 3 ]

Problem - 2: Remove the spaces found in a string

Program Algorithm:

Step-1: Start the program.

Step-2: Declare the str variable with blank spaces in front and back of the string characters.

Step-3: we will use the inbuilt trim() function in Javascript to trim the empty blank spaces in a string.

Step-4: at last, we will store the output in the res_str variable.

Step-5: End the Program.

Program Code:

let str = "     I love Programming      ";
let res_str = str.trim(" ");
console.log(res_str);

Note:-

We will use the trim() while sending input data from the server to the database. For example, when a user needs to give his name in his/her input field named Name, if he/she fills it with spaces in front or back of the name. It will create an issue while storing the data in the database. So, if we use trim() - it automatically trims the empty space and sends the data to the database.

Problem - 3: Celsius to Fahrenheit and Fahrenheit to Celsius

Program Algorithm:

Step-1: Start the Program.

Step-2: To convert celsius to Fahrenheit we will use the formula(i.e., divide the celsius value by 1.8 and add 32 to it.

Step-3: Here, we will use the basic function concept to get the result and at last we will console the result.

Step-4: Procedure to convert Fahrenheit To Celsius and only the formula to convert varies. The formula is we will subtract the Fahrenheit value by 32 and divide it by 1.8.

Step-5: End the Program.

Program Code:

// celsiusToFahrenheit
function celsiusToFahrenheit(d)
{
    return d * 1.8 + 32;
}
let ctf_result = celsiusToFahrenheit(20);
console.log(ctf_result); //68

// fahrenheitToCelsius
function fahrenheitToCelsius(n)
{
    return (n - 32) / 1.8;
}
let ftc_result = fahrenheitToCelsius(68);
console.log(ftc_result); //20

In the next blog, we will explore arrays and their methods in Javascript.

 
Share this