diff --git a/solutions/day-01/destructure_spreading/level1.js b/solutions/day-01/destructure_spreading/level1.js new file mode 100644 index 0000000..c0003c0 --- /dev/null +++ b/solutions/day-01/destructure_spreading/level1.js @@ -0,0 +1,38 @@ +/*Create a function called getPersonInfo. The getPersonInfo function +takes an object parameter. The structure of the object and the output +of the function is given below. Try to use both a regular way and destructuring +and compare the cleanness of the code. If you want to compare your solution +with my solution, check this link.*/ + +/* +Asabeneh Yetayeh lives in Finland. He is 250 years old. He is an Instructor and Developer. +He teaches HTML, CSS, JavaScript, React, Redux, Node, MongoDB, Python and D3.js. He speaks +Amharic, English and a little bit of Suomi(Finnish) +*/ + +const person = { + firstName: 'Asabeneh', + lastName: 'Yetayeh', + age: 250, + country: 'Finland', + job: 'Instructor and Developer', + skills: [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', + 'Python', + 'D3.js', + ], + languages: ['Amharic', 'English', 'Suomi(Finnish)'], +} + +let getPersonInfo = ({firstName, lastName,age,country,job,skills,languages}) =>{ +console.log(`${firstName} ${lastName} live in ${country}. He is ${age} years old. He is an ${job}. +He teaches ${skills[0]}, ${skills[1]}, ${skills[2]}, ${skills[3]}, ${skills[4]}, ${skills[5]}, ${skills[6]}, ${skills[7]}, and ${skills[8]}. +He speaks ${languages[0]}, ${languages[1]} and a little bit of ${languages[2]}.`); +} +getPersonInfo(person); \ No newline at end of file diff --git a/solutions/day-01/destructure_spreading/script.js b/solutions/day-01/destructure_spreading/script.js new file mode 100644 index 0000000..11a57e1 --- /dev/null +++ b/solutions/day-01/destructure_spreading/script.js @@ -0,0 +1,80 @@ +// setInterval + +// - In JavaScript, we use setInterval higher order function to do some +// activity continuously with in some interval of time. The setInterval +// global method take a callback function and a duration as a parameter. +// The duration is in milliseconds and the callback will be always called in that interval of time. +let sayHello = ()=>{ + console.log('Hello'); +} +setInterval(sayHello, 2000); // it prints hello every 2 seconds + +// setTimeout + +// In JavaScript, we use setTimeout higher order function to execute some +// action at some time in the future. The setTimeout global method take a +// callback function and a duration as a parameter. The duration is in milliseconds +// and the callback wait for that amount of time. +let sayGoodBye = ()=>{ + console.log('Goodbye'); +} +setTimeout(sayGoodBye, 3000); + +// Destructuring when we loop through arrays +const countries = [ + ['Finland', 'Helsinki'], + ['Sweden', 'Stockholm'], + ['Norway', 'Oslo'], +]; +for (const [country, city] of countries) { + console.log(country, city); +} + +// Destructuring during loop through an array +const languages = [ + { lang: 'English', count: 91 }, + { lang: 'French', count: 45 }, + { lang: 'Arabic', count: 25 }, + { lang: 'Spanish', count: 24 }, + { lang: 'Russian', count: 9 }, + { lang: 'Portuguese', count: 9 }, + { lang: 'Dutch', count: 8 }, + { lang: 'German', count: 7 }, + { lang: 'Chinese', count: 5 }, + { lang: 'Swahili', count: 4 }, + { lang: 'Serbian', count: 4 }, +] +for (const { lang, count } of languages) { + console.log(`The ${lang} is spoken in ${count} countries.`) +} + +// Destructuring function parameter +const rectangle = { width: 20, height: 10 }; +const calcualteArea = ({ width, height }) => width * height; +const calculatePerimeter = ({ width, height } = 2 * (width + height)); + + +// Spread operator to copy object +// We can copy an object using a spread operator +const user = { + name: 'Asabeneh', + title: 'Programmer', + country: 'Finland', + city: 'Helsinki', +} + +const copiedUser = { ...user } +console.log(copiedUser) + + +// Modifying or changing the object while copying +const users = { + name: 'Asabeneh', + title: 'Programmer', + country: 'Finland', + city: 'Helsinki', +} + +const copiedUsers = { ...user, title: 'instructor' } +console.log(copiedUser) +// {name: "Asabeneh", title: "instructor", country: "Finland", city: "Helsinki"} \ No newline at end of file