Is Sum Less Than Largest In Array

isSumLessThanLargest. SetTimeOut with var and let

by Chintan Upadhayay

JavaScript

//Calculate the Sum of an Array Using the forEach
const sumMe = [1,4,6,7,8,10];

let sumVar = 0;

sumMe.forEach(sumItem => { 
   sumVar += sumItem;
})

/* console.log(sumVar); */

//Math.min() and Math.max() in JavaScript.
const numbers = [10, 20, 23, 5, 40, 54, 80];

const minValue = Math.min(...numbers);
const maxValue = Math.max(...numbers);
console.log(Math.min(4,10));
console.log(`Minimum value is ${minValue} and maximum value is ${maxValue}`);


const originalArray = [1, 2, 3];
const copyArray = [...originalArray];

console.log(originalArray);  // Output: [1, 2, 3]
console.log(copyArray);      // Output: [1, 2, 3]

originalArray.push(4);

console.log(originalArray);  // Output: [1, 2, 3, 4]
console.log(copyArray);      // Output: [1, 2, 3]
//NESTED FOR LOOP WORKING

for(var i=4;i<6;i++){
    for(var j=4;j<5;j++){
      // console.log(i,j);
    }
}
//4 4
//5 4


//Find the Index of the First Occurrence in a String

let haystack = "sadbutsad", needle = "sad";
    console.log("First Occurrence", haystack.indexOf(needle));


//We have numbers array like [2,4,3,5,6,1]; should the addition of any two numbers be less than the largest number of that array in js?

function isSumLessThanLargest(numbers) {
  const largestNumber = Math.max(...numbers);

 for (let i = 0; i < numbers.length-1; i++) {
  
  console.log(i,numbers[i]," ------ ");
  
     for (let j = i + 1; j < numbers.length; j++) {

     console.log("Inner FOr ",j,numbers[i], numbers[j],numbers[i] + numbers[j] );
      /*  if (numbers[i] + numbers[j] >= largestNumber) {
           return numbers[i] +' '+numbers[j];
        }*/
      }
  } 

  return true;
}

const numbersArray = [9,4,3,5,8];
//const result = isSumLessThanLargest(numbersArray);

//console.log(result); // Output: true


//SetTimeOut With Var
//By the time the setTimeout function runs (after the loop has completed), the value of y is 5 because the loop has finished, and y is shared among all the setTimeout callbacks.

//The setTimeout...