DEV Community

KISHAN RAMLAKHAN NISHAD
KISHAN RAMLAKHAN NISHAD

Posted on

js problem

1 . Write a Program to reverse a string in JavaScript

const kk = 'kishan'

console.log(kk.split('').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

Top comments (0)