1. Find the largest element of an array
Here, at first, we assume that the first element of the array is the topper by declaring max = marks then, run a for loop till the last element by array.length. Between the for loop, we put the mark in a variable called element. Next, we checked if the element is greater than the current max mark, then replace the previous max with the current. Thus, after the loop ends we got our desired topper.
2.Remove duplicate item from an array
in the above solution, the name Arif has a duplication. To remove it, firstly, we have declared an empty array to store the unique names after removing the duplication. Then, a for loop will run until the array ends. We stored the names into uniqueName array by checking that if it has a duplication or not, If the name has no duplication then its index will -1. So, only if the index matches with -1 then the number will store in uniqueName array.
3. Count the number of words in a string
okay, let’s easily explain this. How we recognize a word in a sentence? Each word is separated with a single white space. So if we count those spaces of the sentence, then we can easily get the number of words. Let’s do it. There is a string in the variable named speech. We ran a for loop through the string and checked if there is a white space or not or the if there any double spaces between the words. After that, if there is a white space and there is no double space then the word will count by the variable count.
4.Reverse a string
let’s make a function with a parameter that will receive a string. After that, a for loop will run through each letter of the string. And arrange it as the sequence like, the first letter will be added on the last index of the new string.
Now let’s talk about some mathematical problems
5.Factorial of a number using for loop
The factorial function says to multiply all whole numbers from our chosen number down to 1.
Examples: 4! = 4 × 3 × 2 × 1 = 24.
NOTE: The factorial loop will start from 1. Because (Something * 0 = 0)
6. Factorial of a number using recursive function
7. Fibonacci Series using a for loop
The sequence of numbers, starting with zero and one, is created by adding the previous two numbers. For example, the early part of the sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144, 233, 377, and so on.
NOTE: Fibonacci loop will always start from 2 and the first 2 elements will hardcoded. ex: [0,1]
8. Fibonacci series in a recursive way
9. Check whether a number is a Prime Number or not
A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself
10. Check whether a number is an Even/Odd
A number that is divisible by 2 and generates a remainder of 0 is called an even number. An odd number is a number that is not divisible by 2. The remainder in the case of an odd number is always “1”.