Array Question
Q1.Span Of Array
here we need to find the diffrenct between max and min
we have 2 ways
1)sort the array
2)with the help of for loop
- let data = [12,23,232,14];
data.sort(function (a,b) {return a-b})
let diff = data[data.length-1] - data[0];
console.log(diff);
Q2.We have to sort the array according to there concurrency
Q3.Palindrome Checker: Write a function that checks whether a given string is a palindrome (reads the same backward as forward). The function should return true if it is, false otherwise.
function m1(str){
let data = str.split("").reverse().join('');
if(str==data){ return true; }else{ return false; }
}
let res = m1("Ten"); console.log(res);
using the normal loop
// check the string is palindrome or not
function m1(str){ let res = "";
for(let i = str.length;i>=0;i--){ res+=str.charAt(i); } if(res== str){ return true; }else{ return false; } }
let res = m1("nen"); console.log(res);
Q4.Reverse a String: Write a function that reverses a given string without using any built-in reverse functions.
// check the string is palindrome or not
function m1(str,app){ let val = str.split('').reverse().join(''); let res = app.split(' ').reverse().join(' '); console.log(res);
return val; }
let res = m1("Ten","Hi My Name is Slim Shedy"); console.log(res);
using the normal JS
function m1(str,app){ let val = ""; for(let i = str.length;i>=0;i--){ val+=str.charAt(i); } return val; }
let res = m1("Ten"); console.log(res);
Q5.Find the Maximum Value: Write a function that takes an array of numbers as input and returns the maximum value from that array.
let data =[23,4,545,6,7]; // sort
function m1(data){
let sortData = data.sort((x,y)=>y-x); return sortData[0];
let res = m1(data); console.log(res);
using the function
function m1(arr){ let max = arr[0]; // let sortData = data.sort((x,y)=>y-x); // return sortData[0];
for(let i = 0;i<arr.length;i++){ if(max < arr[i]){ max = arr[i]; } } return max; }
let res = m1(data); console.log(res);
Q6.Factorial: Write a function to calculate the factorial of a given number. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
// foctarial
function m1(no){ let data = 0; for(let i = no;i>0;i--){ data += no*no-1; }
return data; }
let res = m1(5); console.log(res);