forked from kipchirchiralb/JavaScript-C2-23
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassingment1.js
More file actions
185 lines (180 loc) · 3.51 KB
/
Copy pathassingment1.js
File metadata and controls
185 lines (180 loc) · 3.51 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
let products = [
{
code: "A001",
name: "T-Shirt",
color: "Red",
size: "Medium",
price: 20,
},
{
code: "B002",
name: "Lipstick",
brand: "MAC",
shade: "Ruby Woo",
price: 15,
},
{
code: "E003",
name: "Headphones",
brand: "Sony",
color: "Black",
price: 50,
},
{
code: "F004",
name: "Chocolate",
brand: "Cadbury",
flavor: "Milk",
price: 5,
},
{
code: "H005",
name: "Cushion",
color: "Blue",
shape: "Square",
price: 10,
},
{
code: "A006",
name: "Jeans",
color: "Black",
size: "32",
price: 35,
},
{
code: "B007",
name: "Nail Polish",
brand: "OPI",
shade: "Bubble Bath",
price: 12,
},
{
code: "E008",
name: "Smartphone",
brand: "Samsung",
color: "White",
price: 500,
},
{
code: "F009",
name: "Cereal",
brand: "Kellogg's",
flavor: "Fruit Loops",
price: 3,
},
{
code: "H010",
name: "Curtains",
color: "Green",
material: "Cotton",
price: 25,
},
{
code: "A011",
name: "Sweatshirt",
color: "Grey",
size: "Large",
price: 30,
},
{
code: "B012",
name: "Mascara",
brand: "Maybelline",
shade: "Blackest Black",
price: 8,
},
{
code: "E013",
name: "Tablet",
brand: "Apple",
color: "Silver",
price: 800,
},
{
code: "F014",
name: "Potato Chips",
brand: "Lays",
flavor: "Original",
price: 2,
},
{
code: "H015",
name: "Throw Blanket",
color: "Pink",
material: "Fleece",
price: 15,
},
{
code: "A016",
name: "Dress",
color: "Blue",
size: "Small",
price: 40,
},
{
code: "B017",
name: "Eyeliner",
brand: "Kat Von D",
shade: "Trooper",
price: 18,
},
{
code: "E018",
name: "Laptop",
brand: "HP",
color: "Silver",
price: 1000,
},
{
code: "F019",
name: "Ice Cream",
brand: "Ben & Jerry's",
flavor: "Chocolate Chip Cookie Dough",
price: 6,
},
{
code: "H020",
name: "Picture Frame",
color: "Gold",
material: "Wood",
price: 8,
},
];
/**
* Create a function that determines the category of a product based on its code. The product codes are as follows:
*
* Code starting with "A": Apparel
* Code starting with "B": Beauty
* Code starting with "E": Electronics
* Code starting with "F": Food
* Code starting with "H": Home
*
* Add a category property to each product object with the value as the correct property category
* */
// create an empty array -- resultProducts
// create a loop to iterate though all the elements in the products array
// for each element in the array, check the starting char of code property and add a category property that suits it
// add the element(product) to the new array(resultProducts)
// for loop
// push()
// charAt(0)
let resultProducts = [];
for (let index = 0; index < products.length; index++) {
let product = products[index];
let firstCharOfProductCode = product.code.charAt(0);
if (firstCharOfProductCode === "A") {
product.category = "Apparrel";
} else if (firstCharOfProductCode === "B") {
product.category = "Beauty";
} else if (firstCharOfProductCode === "E") {
product.category = "Electronics";
} else if (firstCharOfProductCode === "F") {
product.category = "Food";
} else if (firstCharOfProductCode === "H") {
product.category = "Home";
} else {
product.category = "random";
}
resultProducts.push(product);
}
console.log(resultProducts);