Revision of reduce

const products = [
  { id: 1, name: "MacBook Pro", category: "Laptop", price: 180000, isAvailable: true, rating: 4.8 },
  { id: 2, name: "Dell Inspiron", category: "Laptop", price: 90000, isAvailable: true, rating: 4.2 },
  { id: 3, name: "Samsung Galaxy S21", category: "Phone", price: 70000, isAvailable: false, rating: 4.5 },
  { id: 4, name: "iPhone 13", category: "Phone", price: 120000, isAvailable: true, rating: 4.7 },
  { id: 5, name: "Sony Bravia", category: "TV", price: 150000, isAvailable: true, rating: 4.6 },
  { id: 6, name: "LG OLED", category: "TV", price: 170000, isAvailable: false, rating: 4.4 },
  { id: 7, name: "Fossil Smartwatch", category: "Watch", price: 25000, isAvailable: true, rating: 4.1 },
  { id: 8, name: "Casio Digital", category: "Watch", price: 5000, isAvailable: true, rating: 4.0 },
  { id: 9, name: "HP Pavilion", category: "Laptop", price: 75000, isAvailable: true, rating: 4.3 },
  { id: 10, name: "Realme 11 Pro", category: "Phone", price: 33000, isAvailable: true, rating: 4.2 }
];

Tasks -

some() method in array

It returns true if at least one item satisfies the condition. Actually it stops the execution if it finds the first item which satisfies the condition. eg. If someone says you to catch one fish, then you stop after catching one fish.

const numbers = [1,3,5,7,9];
const hasEven = numbers.some(number => number % 2 === 0);
console.log(hasEven);

Tasks -

Check if the array [10,20,30,40,50] has at least a number more than 40.

Check if the array [”apple”,”banana”,”mango”] has at least a string whose length is less than 4.

every() method in array

It returns true only if all the elements satisfies the condition. Actually it stops the execution once it find the item which fails the condition. eg. If you are checking did all students passed in exam and if you find one failed student then you can instantly say that every student doesnt pass in exam.

const numbers = [1,3,5,7,9];
const isOdd = numbers.every(number => number % 2 !== 0);
console.log(isOdd);

Tasks -

Check if all the students are passed in exam or not [45,98,67,89,67,77,33,48,32]. pass marks is 40.

Check if all words are of length more than 4 or not [”apple”,”banana”,”mango”].