Js programming interview questions
1)
var fruits=["Water Melon","Plum","Apple","Grapes"];
let newData=[];
fruits.map(function(fruit){
if(fruit.substr(2).length>3){
// console.log(fruit);
newData.push(fruit);
}
return newData;
})
console.log(newData);
Ans: [ 'Water Melon', 'Grapes' ]
2. function is_domain(str){
regexp=/^[a-z]+([.\:]{1}{a-z0-9]+)+\.[a-z]{2,6}$/
if(str.match(regexp)){
return true;
}
else return false;
}
console.log(is_domain("www.jimbo.5.cn.in"));
console.log(is_domain("http://:google.com"));
console.log(is_domain("www.n5m.co.uKT"));
console.log(is_domain("www.lettySing.com"));
Ans : all false.
1. A new feature of ES6 is the template literal (multiline string)
2. ES6 provides the syntactic sugar to eliminate the redundancy of having to write x: x. You can simply write x once, and it will be converted tox: x (or something equivalent) under the hood. Here is the same function from above rewritten to use this new syntax: const getMousePosition = (x, y) => ({ x, y });
3. Write Concise Declarative Functions with ES6
4. When defining functions within objects in ES5, we have to use the keyword function as follows:
const person = {
name: "Taylor",
sayHello: function() {
return `Hello! My name is ${this.name}.`;
}
};
With ES6, you can remove the function keyword and colon altogether when defining functions in objects. Here's an example of this syntax:
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
Refactor the function setGear inside the object bicycle to use the shorthand syntax described above.
5. Use getters and setters to Control Access to an Object
6. Create a Module Script
<script type="module" src="filename.js"></script>
7. Use export to Share a Code Block
const add = (x, y) => {
return x + y;
}
export { add };
8. Create a JavaScript Promise const myPromise = new Promise((resolve, reject) => { });
Q) Array of array to single array?
let a =[["b","a"],["a","c"]];
let b=[]
for(let i=0;i<a.length;i++){
b.push(...a[i])
}
// b.sort((a,b)=>a<b?1:-1)
for(let i=0;i<b.length;i++){
for(let j=i;j<b.length;j++){
if(b[i]<b[j]){
let temp= b[i]
b[i]= b[j]
b[j]= temp
}
}
}
console.log(b)
Comments
Post a Comment