-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced js lab solution.js
More file actions
53 lines (45 loc) · 1.06 KB
/
advanced js lab solution.js
File metadata and controls
53 lines (45 loc) · 1.06 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
// Step 1: Iterate over an array
var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake'];
function logDairy() {
for (const item of dairy) {
console.log(item);
}
}
// Call the function to log dairy products
logDairy();
// Expected Output:
// cheese
// sour cream
// milk
// yogurt
// ice cream
// milkshake
// Step 2: Iterate over an object's own properties
const animal = {
canJump: true,
};
const bird = Object.create(animal);
bird.canFly = true;
bird.hasFeathers = true;
function birdCan() {
for (const key of Object.keys(bird)) {
console.log(`${key}: ${bird[key]}`);
}
}
// Call the function to log bird's own properties
birdCan();
// Expected Output:
// canFly: true
// hasFeathers: true
// Step 3: Iterate over an object's properties, including prototype
function animalCan() {
for (const key in bird) {
console.log(`${key}: ${bird[key]}`);
}
}
// Call the function to log all properties (own + prototype)
animalCan();
// Expected Output:
// canFly: true
// hasFeathers: true
// canJump: true