Basic coding problems with solution in Javascript

Foysal Ahammed Sami
4 min readNov 5, 2020

01. Find the largest integer of array

First we declare a variable with a value of 0 to hold the largest number then we loop over the array and compare the largest number variable with all of the array element if the element is greater than or equal to the largest number we are reassigning the value. After looping over all the element of array we will get the largest number of the array .

02. Find the largest word in a sentence

There are many ways to find the largest word in a sentence. In this example first we converted the sentence to an array using JavaScript built in split() method. Then we declare a variable to hold the largest word and set it’s value as empty string . Then we loop over the the word Array and then compare between the current element of the loop. If the length of current element is greater than largest word then we will set the current element as largest word .

03. Sum of all numbers in an array

We have given an array. We have to find the sum of the all element of this array. To find the sum we declared a variable and set it’s initial value to 0. Then we loop over the array and added the current element to sum and finally we got the total sum of the array.

04. Sum of all numbers in a range

To solve this problem we have declared a function and it requires two arguments one is from where and to where . Then inside the function we have declared a variable to store the sum . And loop over from given number to the “to” number and adding the current element to the sum. And returning the sum from the function.

05. Remove duplicate elements from an array (not using for loop)

For solving this problem we will use JavaScript filter method . We are checking if the item exists in the array. And then putting all together in a newArray which is the unique elements array.

06. Find even numbers

Even numbers are those numbers which are perfectly divided by 2. Here is in the is is even function we are checking if the number is perfectly divided by 2 or not if it is then we are returning true otherwise false.

07. Find odd numbers

In the case of odd numbers we are doing the same as before described in no-6 difference is we are checking if the number is not divisible with 2 or not . If it is then we are returning true otherwise false.

08. Count the numbers of words in an sentence

To count the number of words in a sentence we have to convert the sentence into a array using the split method the first argument will be “ ”(space) and it will split the sentence into an array . If we return the length of that array we will get the number of words .

09. Reverse a string

To reverse a string or word we have to convert the string into an array then we can use the build in array method to reverse the array and lastly we can join the element of that using built in JavaScript join() method.

10. Truncate a sentence

To truncate a sentence we have to use splice() method it will cut the sentence in specified index of the sentence and return the truncated string

--

--