|
| 1 | +// Let's Practice |
| 2 | +// ------------------------------------------------------------------------------------------------------------------------- |
| 3 | + |
| 4 | +// 1. For a given array with marks of students -> [85,97,44,76,60]. Find the average marks of the entire class. |
| 5 | + |
| 6 | +let marks = [85, 97, 44, 76, 60]; |
| 7 | +let totalsum = 0; |
| 8 | + |
| 9 | +// Add each mark to the total |
| 10 | +for (let value of marks) { |
| 11 | + totalsum = totalsum + value; |
| 12 | +} |
| 13 | + |
| 14 | +console.log(`total marks of the entire class is : ${totalsum}`); |
| 15 | + |
| 16 | +// Calculate average marks |
| 17 | +let avg = totalsum / marks.length; |
| 18 | +console.log(`total average marks of the entire class is : ${avg}`); |
| 19 | + |
| 20 | +// ------------------------------------------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | +// 2. For a given array with prices of 5 items -> [250,645,300,900,50] |
| 23 | +// All items have an offer of 10% OFF on them. Change the array to store final price after applying the offer. |
| 24 | + |
| 25 | +// Calculation example: |
| 26 | +// Applying 10% off on 300 = 300/10 = 30/- off |
| 27 | +// Final price = 300 - 30 = 270 |
| 28 | +// The updated array in this case value will turn out to be 270 after applying an offer of 10%. |
| 29 | +// conclusion: The updated array value will reflect the discounted prices. |
| 30 | + |
| 31 | +// SHOWING 2 WAYS TO SOLVE: |
| 32 | + |
| 33 | +// 1. Using 'for-of' loop (modify the for-in loop and track the index indirectly) |
| 34 | +let items = [250, 645, 300, 900, 50]; |
| 35 | +let index = 0; |
| 36 | + |
| 37 | +for (const value of items) { |
| 38 | + console.log(`value at index ${index} = ${value}`); |
| 39 | + index++; |
| 40 | +} |
| 41 | + |
| 42 | +// 2. Using traditional 'for' loop (directly track the index) |
| 43 | + |
| 44 | +// 1. Solving the question using 'for-in' loop |
| 45 | +let items1 = [250, 645, 300, 900, 50]; |
| 46 | +let index1 = 0; |
| 47 | + |
| 48 | +for (const value of items1) { |
| 49 | + console.log(`value at index ${index1} = ${value}`); |
| 50 | + |
| 51 | + let offer = value / 10; // Calculate 10% discount |
| 52 | + items1[index1] = items1[index1] - offer; // Apply discount |
| 53 | + console.log(`value after offer = ${items1[index1]}`); |
| 54 | + |
| 55 | + index1++; |
| 56 | +} |
| 57 | +console.log(items1); // Final array after discount |
| 58 | + |
| 59 | +// 2. Solving the question using traditional 'for' loop |
| 60 | +let items2 = [250, 645, 300, 900, 50]; |
| 61 | + |
| 62 | +for (let index2 = 0; index2 < items2.length; index2++) { |
| 63 | + console.log(`value at ${index2} = ${items2[index2]}`); |
| 64 | + |
| 65 | + let offer = items2[index2] / 10; // Calculate 10% discount |
| 66 | + items2[index2] = items2[index2] - offer; // Apply discount |
| 67 | + console.log(`value after offer = ${items2[index2]}`); |
| 68 | +} |
| 69 | +console.log(items2); // Final array after discount |
| 70 | + |
| 71 | +// ------------------------------------------------------------------------------------------------------------------------- |
0 commit comments