Some Problem solving with JS

Mdparveg
2 min readNov 5, 2020

Today we will see some problem solution of JavaScript..

1st Find the highest value from any array…

const firstPerson = 550;
const secondPerson = 750;
const thirdPerson = 950;
if(firstPerson> secondPerson){
if(firstPerson> thirdPerson){
console.log('firstPerson is bigger');
}
else{
console.log('thirdPerson is bigger');
}
} else{
if(secondPerson > thirdPerson){
console.log('secondPerson is bigger');
}}

the alternate way is…

const firstPerson = 550;
const secondPerson = 750;
const thirdPerson = 950;
const max = Math.max(firstPerson, secondPerson, thirdPerson)
console.log(max);

for marks…

const marks = [12, 43, 54, 63, 56, 76, 75, 3, 78, 98]
let max = marks[0];
for (let i = 0; i < marks.length; i++) {
const element = marks[i];
if (element > max) {
max = element;
}}
console.log(max);

2nd Reverse a string…

function reversestring(name) {
let reverse = "";
for (let i = 0; i < name.length; i++) {
let reverse = "";
let char = name[i];
reverse = char + reverse
}
return reverse
}
const name = "Hello I am Parveg"
console.log(reverseString(name));
// expected output is gevrap ma I olleH

the alternate and easy way is

function stringReverese(str) {
return str.split('').reverse().join('');
}
stringReverese('Hello World!');
// Should Return: !dlroW olleH

3rd Sum of all numbers in an array…

function summation(num) {
let sum = 0;
for (let i = 0; i < num.length; i++) {
const element = num[i];
sum = sum + element
}
return sum
}
const add = summation([12, 43, 10, 63, 56, 34, 75, 3, 78, 98])
console.log(add);

4th Remove duplicate item from an array…

const duplicate = [12, 43, 54, 23,45,65,67,32,12,3,12,3,45,65,24];
let uniqueNum = []
for (let i = 0; i < duplicate.length; i++) {
const element = duplicate[i];
const index = uniqueNum.indexOf(element)
if(index == -1){
uniqueNum.push(element)
}}
console.log(dup.length);

5th Count the number of words in a string…

const speech = 'i am a good person.'
let count = 0;
for (let i = 0; i < speech.length; i++) {
const char = speech[i];
if(char == " "){
count++;
}}
console.log(count);
console.log(speech.length);

6th Factorial with for loop…

function factorial (num){
let factorial = 1;
for(let i = 1; i <= num; i++){
factorial = factorial*i
}
return factorial
}
const res = factorial(5);
console.log(res); // expected output is 120

7th Factorial with while loop…

function factor(num){
let i = 1;
let factor = 1;
while(i <=num){
factor = factor*i;
i++;
}
return factor;
}
let result = factor(6);
console.log(result) //expected output 720

8th Factorial with recursive method…

function fact(n){
if(n ==0){
return 1;
}
else{
return n* fact(n-1);
}}
var result = fact(5);
console.log(result); //expected output 120

9th Make a Fibonacci Series using a for loop…

function fibonacci(n){
var fibo = [0, 1];
for(var i= 2; i <= n; i++){
fibo[i] = fibo[i-1] + fibo[i-2];
}
return fibo;
}
var result = fibonacci(12);
console.log(result); //expected output [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ]

10th Make Factorial in a Recursive function…

function fibonacci(n){
if(n == 0){
return 0;
}
if(n ==1){
return 1;
}
else{
return fibonacci(n-1) + fibonacci(n-2);
}}
var result = fibonacci(12);
console.log(result);

That’s all for today…

Thank you..

--

--