From fd14481d80735abb32c7a3aff4307bcdde02fb22 Mon Sep 17 00:00:00 2001 From: Bhavya Khatri Date: Thu, 3 Aug 2023 23:21:13 +0530 Subject: [PATCH] objects --- Objects/index.html | 20 ++++++ Objects/interview.js | 161 +++++++++++++++++++++++++++++++++++++++++++ Objects/script.js | 59 ++++++++++++++++ Objects/style.css | 8 +++ 4 files changed, 248 insertions(+) create mode 100644 Objects/index.html create mode 100644 Objects/interview.js create mode 100644 Objects/script.js create mode 100644 Objects/style.css diff --git a/Objects/index.html b/Objects/index.html new file mode 100644 index 0000000..56a1332 --- /dev/null +++ b/Objects/index.html @@ -0,0 +1,20 @@ + + + + + + + Object Destructuring, Object Referencing, Spread and Rest Operators, + Shallow vs Deep Copy + + + + +

+ Object Destructuring, Object Referencing, Spread and Rest Operators, + Shallow vs Deep Copy +

+ + + + diff --git a/Objects/interview.js b/Objects/interview.js new file mode 100644 index 0000000..d8b3347 --- /dev/null +++ b/Objects/interview.js @@ -0,0 +1,161 @@ + + +// Question 1 - What is the output + +/*const obj = { + a: "one", + b: "two", + a: "three" +} + +console.log(obj); */ + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + + +// Question - 2 => Create a Function multiplyByTwo(obj) that multiplies all numeric property values of nums by 2. + + +/*let nums = { + a: 100, + b: 200, + title: "My Nums", +} + + +multiplyByTwo(nums) + +function multiplyByTwo(obj) { + for (key in obj) { + if (typeof obj[key] === "number") { + obj[key] *= 2; + } + } +} + +console.log(nums); */ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 3 => What is the output of following code + +/* const a = {} +const b = {key: "b"} +const c = { key: "c" } + +a[b] = 123; +a[c] = 456; + +console.log(a[b]); */ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 4 => What is JSON.stringify and JSON.parse? + +/* const user = { + name: "Bhavya", + age: 24 +} + +const strObj = JSON.stringify(user); + +console.log(strObj); + +localStorage.setItem("test", strObj) + +console.log(JSON.parse(strObj)); */ + + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 5 => What is the output? + +// console.log([..."Alwar"]); + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 6 => What is the output? + +/*const user = { name: "Bhavya", age: 24 }; +const admin = { admin: true, ...user } + + +console.log(admin); */ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 7 => What is the output? + + +/* const settings = { + name: "Bhavya", + level: 19, + health: 90, +} + +const data = JSON.stringify(settings, ["level", "health"]) +console.log(data); */ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 8 => What is the output? + +/* const shape = { + radius: 10, + diameter() { + return this.radius * 2; + }, + perimeter: () => 2 * Math.PI * this.radius, +}; + +console.log(shape.diameter()); +console.log(shape.perimeter()); */ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 9 => What is the destructuring in JavaScript? + +/* let user = { + name: "Bhavya", + age: 24, + fullName: { + first: "Alex", + last: "jones", + }, +} + + +const name = user + +const {fullName: {first} } = user + + +console.log(first); */ + + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Question - 10 => What is the output? + + +/* let c = { greeting: "Hey!" }; +let d; + + +d = c; +c.greeting = "Hello"; +console.log(d.greeting); */ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + + + diff --git a/Objects/script.js b/Objects/script.js new file mode 100644 index 0000000..e91fd77 --- /dev/null +++ b/Objects/script.js @@ -0,0 +1,59 @@ +// Objects in JavaScript + +// IIFE + + +/*const func = (function (a) { + delete a; + return a; +})(5); + +console.log(func); + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +/*const user = { + name: "Bhavya Khatri", + id: 9315, + "How are you Bhavya": "I am Fine", +}; + +delete user["How are you Bhavya"]; + +console.log(user.id); */ + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + +// Dynamic Key value pairs + +/*const property = "first name" +const name = "Bhavya Khatri" +const empId = 9315 +const empEmailId = "bhavya.khatri@489mango.com" + +const users = { + [property]:name, empEmailId, empId +} + + +console.log(users);*/ + + +/* ------------------------------------------------------------------------------------------------------------------------------------ */ + + +// Looping through objects + + +/*const User = { + name: "Bhavya Khatri", + id: 9315, + "Good": true +}; + + +for (key in user) { + console.log(User[key]); +} +*/ + diff --git a/Objects/style.css b/Objects/style.css new file mode 100644 index 0000000..6426de3 --- /dev/null +++ b/Objects/style.css @@ -0,0 +1,8 @@ +body { + background-color: lightcyan; + font-family: Verdana, Geneva, Tahoma, sans-serif; +} + +h1 { + text-align: center; +}