-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (118 loc) · 3.59 KB
/
index.js
File metadata and controls
138 lines (118 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Function Basics: Write a function called addNumbers that takes two parameters and returns their sum.
function addNumbers(a,b) {
return a + b
}
console.log(addNumbers(3,8))
// Object Creation: Create an object called person with properties name, age, and gender. Assign values to these properties and print the object.
let person = {
name:"Sam",
age: 10,
gender: "female"
}
console.log(person)
// Function with Object Parameter: Write a function called printPerson that takes a person object as a parameter and prints out their name, age, and gender.
let Person = {
name: "Samuel",
age: 20,
gender: "Male"
}
function printPerson(Person){
console.log(Person.name)
console.log(Person.age)
console.log(Person.gender)
}
printPerson(Person)
// Object Modification: Create a function called incrementAge that takes a person object as a parameter and increments the age property by 1.
let persoN = {
name:"kofi",
age: 24,
gender: "male"
}
function incrementAge(persoN) {
persoN.age += 1
console.log(persoN.age)
}
incrementAge(persoN)
// Object Cloning: Write a function called cloneObject that takes an object as a parameter and returns a new object with the same properties and values.
let meal = {
food: "jollof",
drink: "fanta",
price: 100
}
function cloneObject(meal){
return meal
Object.assign(newMeal,meal)
console.log(newMeal)
}
console.log(cloneObject(meal))
// Object Comparison: Create two person objects, compare them, and print whether they are equal or not based on their properties.
let person1 = {
weight: "100kg",
height: "6ft",
complexion: "dark"
}
let person2 = {
weight: "100kg",
height: 6,
complexion: "dark"
}
function compareObjects(person1,person2){
return(
person1.weight === person2.weight &&
person1.height === person2.height &&
person1.complexion === person2.complexion
)
}
if (compareObjects(person1,person2)) {
console.log("Both objects are equal")
}
else{console.log("objects are not equal")}
// Function as Object Property: Create an object called calculator with properties add and subtract, which are functions that take two parameters and perform the respective operations.
let calculator = {
add: function add(a,b) {
return a + b
},
subtract: function subtract(x,y) {
return x - y
}
}
console.log(calculator.add(5,5))
console.log(calculator.subtract(13,7))
// Object Looping: Write a function called printObject that takes an object as a parameter and prints out all its key-value pairs.
let me = {
name: "Ama",
age: 30,
location: "Kumasi"
}
function printObject(me){
for (let key in me) {
return me
}
}
console.log(printObject(me))
// Object Deletion: Create a function called deleteProperty that takes an object and a property name as parameters and deletes that property from the object.
let boy = {
name: "kofi",
age: 5,
location: "Ghana"
}
function deleteProperty(boy,name) {
delete(boy.name)
}
console.log(deleteProperty(boy,name))
console.log(boy)
// Deep Object Cloning: Write a function called deepClone that takes an object as a parameter and returns a new object with all nested objects cloned as well.
let house = {
color: "cream",
storey: 2,
rooms: {
bedroom: 4,
livingRoom: 1,
storeRoom: 2
}
}
function deepClone(house){
let newHouse = structuredClone(house)
return newHouse
}
console.log(deepClone(house))