DEV Community

KISHAN RAMLAKHAN NISHAD
KISHAN RAMLAKHAN NISHAD

Posted on • Edited on

js problem

1 . Write a Program to reverse a string in JavaScript

const kk = 'kishan'

console.log(kk.split('').reverse().join(''))

without split method 

const ll = []

for (let i= 0 ; i < kk.length ; i++){
    ll.push(kk[i])
}

console.log(ll.reverse().join(''))
Enter fullscreen mode Exit fullscreen mode

2.Write a Program to check whether a string is a palindrome string.

ANS: home work

  1. find the largest number in array
const  array = [30,100.3000,34,99999,10] 

function kk(array){
    let largest  = array[0]

    for(let i = 0 ; i< array.length ; i ++){

        if(array[i]>largest){
            largest = array[i];
        }
    }
     console.log(largest)
}
kk(array)
Enter fullscreen mode Exit fullscreen mode
  1. how to empty array in javascript
=> array.length = 0

console.log(array);
Enter fullscreen mode Exit fullscreen mode

Using while with pop()
const array = [30, 100, 3000, 34, 99999, 10];
while (array.length > 0) {
    array.pop();
}
console.log(array); // Output: []

3. Using splice()

const array = [30, 100, 3000, 34, 99999, 10];
array.splice(0, array.length);
console.log(array); // Output: []

Using a for loop:

const array = [30, 100, 3000, 34, 99999, 10];

for (let i = array.length - 1; i >= 0; i--) {
    array.pop();
}

console.log(array); // Output: []



Enter fullscreen mode Exit fullscreen mode

5 how would you check if a number is a interger

her i have 2 way

first id inbuilt function i.e Number.isInteger(12)

second way

if(number% 1 === 0 ){
    console.log(true )
}else{
    console.log(false  )

}
Enter fullscreen mode Exit fullscreen mode
  1. make a dublicate of array
var a = [1,2,3]

// console.log(a.concat(a));



function duplicate (a){
    return a.concat(a)
}
console.log(duplicate(a));
Enter fullscreen mode Exit fullscreen mode

--- function ------

  1. remove dublicate elemet from array
let array = [1,2,3,4,4,5]


// console.log(array.indexOf(5))


a.  console.log(... new Set(array))

b . function findublicate(array){
    const dublocate  = array.filter((value,index,ar) => {
    return ar.indexOf(value)=== index
})

console.log(dublocate)
}

findublicate([1,3,4,4,5,6])
Enter fullscreen mode Exit fullscreen mode
  1. write a JavaScript function that reverse a number
function reversnumberkaro(num){
    return Number(num.toString().reverse().join(""))

}

console.log(reversnumberkaro(25))
Enter fullscreen mode Exit fullscreen mode

8.write a JavaScript function that check whether a passed string is palindrome or not

function strcheckpalin(str){

   const reversed =  str.split().reverse().join('')

   if(reversed === str){
    console.log("dhbdbdh");

    return true
   }else{
    return false
   }


}

strcheckpalin('poop')
Enter fullscreen mode Exit fullscreen mode

9.write a javascript function that return a passed string with letter in alphabetical order

function strinaplfa(str){
  return str.split("").sort().join("")
}

console.log(strinaplfa("kishan"));
Enter fullscreen mode Exit fullscreen mode
  1. loop an array and add member of it

let array =[1,2,3]

var sum = 0

array.forEach((e)=> {
    sum = sum +e
})

console.log(sum);

console.log(array.reduce((a,b)=> a+b ,0));

Enter fullscreen mode Exit fullscreen mode
  1. in an array of number and string , add only those member which are not string

let array  = ['mshbhdsv',122, 'sdfawef',3]

var sum = 0
 array.forEach( (e)=>{
    if(typeof e === 'number'){
        sum = sum +e
    }


 })
 console.log(sum);

Enter fullscreen mode Exit fullscreen mode
  1. loop an array of object and remove all the object don't have gender values male

  2. check every elemnt in array 1 is square present in secind array

const arr1 = [2, 3, 4];
const arr2 = [4, 9, 16];

let isSquarePresent = true;

for (let num of arr1) {
    if (!arr2.includes(num * num)) {
        isSquarePresent = false;
        break; // Exit loop early if any square is missing
    }
}

console.log(isSquarePresent); // Output: true
Enter fullscreen mode Exit fullscreen mode

another method

const arr1 = [2, 3, 4];
const arr2 = [4, 9, 16];

const result = arr1.every(num => arr2.includes(num * num));

console.log(result); // Output: true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)