Basic 10 Problems Solving With JavaScript That should JavaScript Developer Practice

Md. Mostafa Al Mahmud
3 min readNov 4, 2020

--

JavaScript consists of a standard library of objects, i.e., Array, Math, and Date, and a core set of programming language elements such as operators, statements, and control structures.

javascript problem solving

JavaScript is an interpreted, lightweight, or just-in-time (JIT) compiled programming language with first-class functions. Every type of problems can be solved through this language.

Today I am going to solve the best 10 problems with the help of JavaScript as well.

Let’s get started!

1. Find the largest element of an array

const arr = [1, 5, 7, 3, 11, 9, 99, 57];let largest= 0;

for (i = 0; i <= largest; i++){
if (arr[i]>largest) { largest=arr[i]; }
}

console.log(largest); // 99

2. Find the sum of all number in an array

// Using reduce array helper method
const arr = [5, 6, 7, 9, 11]
const sum = arr.reduce((a, b) => a + b, 0)console.log(sum); // 38

3. Remove duplicate item from an array

// Using Set method
let names = ['Mostafa', 'Mahmud', 'Shams', 'Mostafa', 'Sadia'];
let uniqueNames= [...new Set(names)];console.log(uniqueNames);
// ['Mostafa', 'Mahmud', 'Shams', 'Sadia']

4. Count the number of words in a string

// Using match function with regex
const words = "I love JavaScript";
let wordsCount = words.match(/(\w+)/g).length;console.log(wordsCount);
// 3

5. Reverse a string

// Using for loop with variable decreament from last to first
function reverseStr(str) {
let newStr = ""; for (let i = str.length - 1; i >= 0; i--) { newStr += str[i]; } return newStr;}console.log(reverseStr('Bangladesh'));
// hsedalgnaB

6. Calculate Factorial of a number using for loop

function factorial(num) {    // 0 or 1 factorial is always 1    if (num === 0 || num === 1)       return 1;    for (let i = num - 1; i >= 1; i--) {       num *= i;    }    return num;}console.log(factorial(5));
// 120

7. Calculate Factorial of a number using a while loop

function factorial(num) {  let result = num;  if (num === 0 || num === 1)      return 1;   // When num is less than 1, looping is stopped
while (num > 1) {
num--;
result *= num;

}
return result;
}
factorial(5); // 120

8. Calculate Factorial in a Recursive function

// factorial function runs itself until finished
function factorial(x) {

if (x === 0) {
return 1;
}
return x * factorial(x-1);
}
console.log(factorial(5)); // 120

9. Create a Fibonacci Series using a for loop

let fibo = function(num) {   let a = 0, b = 1, fib = 1;   for(let i = 2; i <= num; i++) {      fib = a + b;      a = b;      b = fib;   }   return fib;};console.log(fibo(5)) // 5console.log(fibo(10)) // 55console.log(fibo(20)) // 6765

10. Check whether a number is a Prime Number or not

// Using ES6 syntax
const isPrime = num => {
for(let i = 2; i < num; i++) { if(num % i === 0) return false;
}
return num > 1;}console.log(isPrime(10)); // falseconsole.log(isPrime(5)); // true

--

--

Md. Mostafa Al Mahmud
Md. Mostafa Al Mahmud

Written by Md. Mostafa Al Mahmud

Full-Stack Software Engineer || JavaScript Lover || Experienced with MERN Stack || Mongo, Express, React, Node, ES6, Netlify, Heroku, Firebase, Version Control