10 Basic Problems in JavaScript That Every Beginner should solve

Largest element of an array:

Finding the largest value of an array is very simple. First declare a variable max and initialize it with the safest minimum value because if you set initial value max = 0. then you will not get the correct value in case of all negative value. after initialize the max variable just go through the entire array and check if the current element of the array is grater or less than max. that’s it.

const arr = [5,10,2,3,41,50,1]let max = Number.MIN_SAFE_INTEGERfor (let i = 0; i < arr.length; i++) {    if (arr[i] > max) {        max = arr[i]
}
}
console.log(max);

Finding Even Odd:

Finding a number is even or odd is the most basic problem you will solve today.you just need to use a conditional statement for this problem.

function evenOdd(num){   if (num %2 ==0) {
return "Even"
}
return "Odd"}evenOdd(10);

Sum of all numbers of an array:

For find the sum of all number of an array you just have to go through the entire array and add every element with the sum

const arr = [5, 10, 2, 3, 41, 50, 1]let sum = 0;for (let i = 0; i < arr.length; i++) {    sum += arr[i]}console.log(sum);

Prime number:

prime number is any natural number which is grater than 1 and can not be divide by any other number except itself. we can easily solve this by writing the following function

function isPrime(number) {    const limit = Math.ceil(Math.sqrt(number))    for (let i = 2; i < limit; i++) {        if (number % i == 0) {           return false;
}
}
return number > 1
}
isPrime(5);

Finding the largest string of an array:

For finding the largest string from an array you just have to follow 3 steps

const LargestString = items => {       let largest = '';       items.forEach(item => {             if (item.length > largest.length) {                    largest = item;
}
});
return largest;
}
const arr = ["Java", "Python", "JavaScript", "C#"]LargestString(arr);

Remove Duplicate item from array:

For removing duplicates values from an array there are so many Ways. you can use indexOf() method and check the return value. You can use set(), filter() etc.

const arr = [5, 10, 2, 50, "r", 41, "r", 2, 50, 1]let checking = {}let newArray = []for (let i = 0; i < arr.length; i++) {        let item = arr[i]        if (checking[item] !== -1) {               checking[item] = -1               newArray.push(item)            }
}
console.log(newArray);

Number of words in a string:

Finding the total number of words in a given string is a little bit tricky. you have to use Regular Expression. Because your string may have space in the starting or in the end, so you have to eliminate those space and also you string may have multiple space between them so also have to convert them into single space. then you will just split them by space

let str = "   hello, my name is    sikder rayhan kabir   "str = str.replace(/(^\s*) || (\s*?)/gi, "")str = str.replace(/[ ]{2,}/gi, " ")str = str.replace(/\n /, "\n")const words = str.split(" ")console.log(words.length);

Reverse a String:

Reversing a string is very easy in JavaScript. just write a for loop which will start form the last index of the string towards the first index and store them in a new string

const str = "Hello, i love javaScript"const len = str.lengthlet revstr = ""for (let i = len - 1; i >= 0; i--) {

revstr += str[i]
}console.log(revstr);

Calculate factorial of a number:

You can find factorial of a number in different Ways. you can use for loop, while loop but for this example i will use recursion. we know that we can find factorial using this formula = n * (n — 1)…* 1

function Factorial(number) {     if (number === 0 || number === 1) {         return 1
}
let total = number * Factorial(number - 1) return total}const a = Factorial(4)console.log(a);

Fibonacci series:

we can find Fibonacci series by using loops but i will go with recursion because recursion make the time complexity linear

function FindFibonacci(limit) {    let fib = "0 1 "    function Fibonacci(first, second) {        let sum = first + second;        if (sum >= limit) {            return;
}
fib += `${sum} ` Fibonacci(second, sum)
}
Fibonacci(0, 1) return fib
}
FindFibonacci(50);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raihankabir

Raihankabir

A self-motivated and enthusiastic web developer with a deep interest in JavaScript.