Code with JS

Find the largest element of an array

Khadiza Rafa
4 min readNov 5, 2020
max = 555

Here I have declared an array of numbers and added a for each loop to traverse all the numbers and compare it to max. The initial value of max here is -999 . A teranary operation where I have compared max and current array element. If true updated max or left unchanged.

Sum of all numbers in an array

Here I have declared a sum variable and set 0 in it. Used a for each loop and added element with the sum variable.

sum = 136

Remove duplicate item from an array

Here I have used an array of numbers where the number 2 appeared twice.What I ahve done I have declared a new array . while traversing through the array I have filter and kept a count of it. Filter will return you an array of elements where the condition provided in it is true, so I just kept the length of it. If the length is equal to 1 that means element appeared once in the array.If so I have pushed it to the new array.

Count the number of words in a string

Here I have split the string using ‘ ‘ . Split function will always return an array of sub strings. Then you just need to find the length of the array to count words.

Reverse a string

Here I have took a variable named reverse and just looped through the string in reversed order. Then concated each element with the variable.

Calculate Factorial of a number using for loop

here I have kept a varibale for which I will determine factorial. Then looped from 1 upto that number and each time multiplied it with the result.

10! = 1 * 2 * 3 * 4 * ..... * 10

Calculate Factorial of a number using a while loop

Here I have used while loop instead of for loop . Added another variable for while loop.

Calculate Factorial in a Recursive function

Here I have used recusion for calculating factorial. In recursion, first you have to set the condition to get out of the loop. That is the first condition if n is that means we have reached to the last element . Here, we are generating factorial in reverse order. When n not equal to 1

recursion(10)  
if (10 === 1) //contion false
return 1
else
return 10 * recursion(9) //it will call recursion untill n-1 = 1
10! = 10 * 9 * 8 * .... * 1

when 1 found it will start returning value to all the functions.

Create a Fibonacci Series using a for loop

Here I have set first and second value and the third value would be the sum of previous two values. This is a very simple code where each time I have updated the value of first and second to generate the third element.

Get Fibonacci value in a recursive way

In the previous example we have shown fibonacci of ten numbers. In this example it’s done in a recursive way, where if the number is 1 or 2 I have sent 0 and 1. Its done because if the index react to 2 or 1 there should be a fixed value 1 and 0.

1 2 3 4 5 6 7 8  9  10//array index
0 1 1 2 3 5 8 13 21 34//fibonacci series

Check whether a number is a Prime Number or not

I have created a flag variable which will be set to true if the remainder of num divided by 2 is zero else it would be consided as an odd number.

--

--