Day 9 — ES6+ patterns

Hands-on

Topic 1 Part 1
const item = "laptop"
const price = 60000;
const tax  = 0.18 
const str = `The ${item} costs ${60000} + ${10800} GST = ${price+10800}`
console.log(str)

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 2 Part 2
const scores = [88, 75, 92, 60, 45];

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 3 Part 3
const [top, second, ...others] = scores;
console.log(top)
console.log(second)
console.log(others)

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 4 Part 4
const user = { name: "Anaya", age: 21, address: { city: "Jaipur", pincode: "302001" } };
const {name, age : userage , address : {city}} = user;
console.log(name,userage,city)

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 5 Part 5
function sumAll(...numbers) {
    return numbers.reduce((a,c) => a+c , 0);
}
console.log(sumAll(1, 2, 3))
console.log(sumAll(10, 20, 30))
console.log(sumAll())

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 6 Part 6
function joinNames(seperator, ...names) {
    return names.join(seperator);
}

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 7 Part 7
console.log(joinNames(", ","priya","arav","kumar"));

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 8 Part 8
const defaults = { theme: "light", lang: "en", notifications: true };
const userPrefs = { theme: "dark", fontSize: 16 };
const merged = {...defaults,...userPrefs}
console.log(merged)

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).

Section 9 Part 9
function applyprefs(defaults,prefs) {
    return {...defaults,...prefs}
}
console.log(applyprefs(defaults,userPrefs))

Try the steps, then compare with ../js/hands-on.js (loaded at the bottom of this page).