-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.js
26 lines (26 loc) · 1.05 KB
/
22.js
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
"use strict";
//22.Intentional Error: If you haven’t received an array index error in one of your programs yet, try to make one happen. Change an index in one of your programs to produce an index error. Make sure you correct the error before closing the program.
Object.defineProperty(exports, "__esModule", { value: true });
// Function to create a fruit object
function createFruits(name, colour, taste) {
return {
name,
colour,
taste,
};
}
;
const fruits = [
createFruits("Apple", "Red", "Sweet"),
createFruits("Banana", "Yellow", "Sweet"),
createFruits("Orange", "Orange", "Citrusy"),
createFruits("Grapes", "Purple/Green", "Sweet"),
createFruits("Mango", "Yellow", "Sweet"),
];
// Access an invalid index
const invalidIndex = 10; //There are only 5 elements in the array, so this will cause an error
console.log(`Fruit at index ${invalidIndex}:`, fruits[invalidIndex]);
// Print the fruits
fruits.forEach((fruits) => {
console.log(`Name: ${fruits.name}, Colour: ${fruits.colour}, Taste: ${fruits.taste}`);
});