JavaScript coding Interview Question

 


JS CODE PRACTICE INTERVIEW QUESTION ES6 (also known as ECMAScript 2015) is the sixth edition of the ECMAScript language specification, which is used to define the scripting language used by web browsers. It introduced many new features and syntax improvements to JavaScript that make it easier and more intuitive to write complex applications. In this article, we'll explore some of the most important concepts introduced by ES6 and how they can be used to write better JavaScript code. ES6 JS => let and const Arrow functions Classes Destructuring Template literals Spread operator Default parameters Rest parameters Object shorthand notation Promises Modules Maps and Sets Iterators and Generators. 1. Find a max element in the array using reduce method. const arr=[1,7,3,48,10,39]; const data=arr.reduce((acc,curr)=>{ if(acc<curr){ acc=curr } return acc },0) console.log(data); o/p => 48 2. Counting occurrences of values in an array of objects based on a specific property. const arr=[ {name:'JOHN',age:23}, {name:'DEV',age:30}, {name:'Vikas',age:10}, {name:'SONU',age:23} ]; const data=arr.reduce((acc,curr)=>{ if(acc[curr.age]){ acc[curr.age]=++acc[curr.age] }else{ acc[curr.age]=1 } return acc },{}) console.log(data); o/p => { '10': 1, '23': 2, '30': 1 } 3. Removing duplicates from an array while preserving the order of elements. let a=[1,2,3,2,"4","2",1,5,6,7,4,6,"s", "a", 4,65,23,222]; var obj = {}; let b=a.filter((item)=>{ return obj.hasOwnProperty(item) ? false : obj[item] = true; }); console.log(b); o/p=====> [1, 2, 3, '4', 5, 6, 7, 's', 'a', 65, 23, 222] 4. Sorting using javascript ===============> 1st method ===> function compareSort(a,b){ if(a>b) return 1; if(a==b) return 0 if(a<b) return -1 } let a=[1,2,15,3,16,4]; a.sort(compareSort); console.log(a); 2nd method => Best way to sort array in JS=> let a=[1,2,15,3,16,4]; a.sort((a,b)=>a-b); console.log(a); o/p => [1,2,3,4,15,16] 5. Counting the occurrences of words in a string using JavaScript. let str="Hello JS Hello Java Hello JS Hello PHP java Hello" let obj={} let arr=str.split(" "); arr.map(res=>{ if(obj[res]==undefined){ obj[res]=1 }else{ obj[res]++ } }) console.log(obj); O/P => { Hello: 5, JS: 2, Java: 1, PHP: 1, java: 1 } 6. Reversing the order of characters in each word of a string using JavaScript. let str="Hello world" let result=str.split(" ").map(res=>res.split("").reverse().join("")) console.log(result) o/p = > [ 'olleH', 'dlrow' ] 7. Finding Similarities Between Two Arrays in JavaScript. const similarity = (arr, values) => arr.filter(v => values.includes(v)); let s=similarity([1, 2, 3], [1, 2, 4]); console.log(s); o/p => [1,2]

Comments