Refactor the code:
var name = "Francis";
var lastname = "Jones"
var age = 23;
var obj
function createObject(name,lastname,age){
obj = {
name:name,
lastname:lastname,
age:age,
}
return obj;
}
console.log(createObject(name,lastname,age))We want to run a function that greets the user by his name, but if the name is not provided we want to show a default name. Old ES5 code:
function greeting(name){
if(name === undefined){
name = 'user'
}
return 'Hello '+ name;
}
console.log(greeting())Refactor the code using ES6 and the rest operator.
function totalDistance(distance1,distance2,distance3){
var distances = [distance1,distance2,distance3]
var total = 0;
for(var i = 0; i < distances.length;i++){
total += distances[i]
}
return total;
}
console.log(totalDistance(200,100,200))Refactor the code:
function showItems(arg1,arg2,arg3){
var arr = [arg2,arg3].concat(arg1)
console.log(arr)
}
showItems(["dogs","cats"],"turtles","sharks");function getLocation(city,country,continent){
if(typeof country === 'undefined'){
country = 'Italy'
}
if(typeof continent === 'undefined'){
continent = 'Europe'
}
console.log(continent,country,city)
}
getLocation('Milan')
// Europe Italy Milan
getLocation('Paris','France')
// Europe France Parisfunction calculatePrice(total, tax = 0.1, tip = 0.05){
// When no value is given for tax or tip, the default 0.1 and 0.05 will be used
return total + (total * tax) + (total * tip);
}indexOf()slice()toUpperCase()toLowerCase()
New Methods:
startsWith()
const code = "ABCDEFG";
console.log(code.startsWith("ABB"));
// false
console.log(code.startsWith("abc"));
// false, startsWith is case sensitive
console.log(code.startsWith("ABC"));
// trueendsWith()
const code = "ABCDEF";
console.log(code.endsWith("DDD"));
// false
console.log(code.endsWith("def"));
// false, endsWith is case sensitive
console.log(code.endsWith("DEF"));
// trueincludes()
const code = "ABCDEF"
console.log(code.includes("ABB"));
// false
console.log(code.includes("abc"));
// false, includes is case sensitive
console.log(code.includes("CDE"));
// truerepeat()
let hello = "Hi";
console.log(hello.repeat(10));
// "HiHiHiHiHiHiHiHiHiHi"var person = {
first: "Alberto",
last: "Montalesi"
}
var first = person.first;
var last = person.last;
console.log(first,last);const person = {
name: "Alberto",
last: "Montalesi",
links:{
social: {
facebook: "https://www.facebook.com/alberto.montalesi",
},
website: "http://albertomontalesi.github.io/"
}
}
const { facebook } = person.links.social;
console.log(facebook);Run code below
var fruits = ['apple','banana','orange'];
for (var i = 0; i < fruits.length; i++){
console.log(fruits[i]);
}
// apple
// banana
// orangeconst fruits = ['apple','banana','orange'];
for(const fruit of fruits){
console.log(fruit);
}
// apple
// banana
// orangeObject.keys()Object.entries()
const car = {
maker: "BMW",
color: "red",
year : "2010",
}
for (const prop of Object.keys(car)){
const value = car[prop];
console.log(prop,value);
}let list = [4, 5, 6];
// for...in returns a list of keys
for (let i in list) {
console.log(i); // "0", "1", "2",
}
// for ...of returns the values
for (let i of list) {
console.log(i); // "4", "5", "6"
}const name = "Alberto";
const surname = "Montalesi";
const age = 25;
const nationality = "Italian";
const person = {
name: name,
surname: surname,
age: age,
nationality: nationality,
}
console.log(person);