Skip to main content

Posts

Showing posts with the label Arrays

JavaScript Interview Questions

JavaScript Interview Questions: 1. Input: array=[3,4,5,6,7,8,9] Output: 4,5,6,7,8,9,10 2. var obj={name:"test"} Replace the the text of div with the value of  name key from an above JavaScript object. <div id="test">This is a text</div> 3. for(var i=0; i<3; i++){     setTimeout(() =>console.log(i) },2000); What it will print in console of the browser?

Find the index of a matching element from an array.

 Get the index of a matching element from an array: Method 1: using without any method (Original code)- let cars = ["BMW", "Volvo", "Mini"]; let element = 'Volvo'; for( let i= 0; i <= cars.length; i++){     if(cars[i] === element){         console.log(i);         return i;     } else {         console.log(-1);         return -1;     } } Output:  1 Method 2: using indexOf  method - let cars = ["BMW", "Volvo", "Mini"]; let element = 'Volvo';     if(cars.indexOf(element) == -1){         console.log("-1");     } else {          console.log(cars.indexOf(element));     } Output:  1 Method 3: using indexOf & includes  method - let cars = ["BMW", "Volvo", "Mini"]; let element = 'Volvo';     if(!cars.includes(element)){         console.log("-1");  ...