adjacentElementsProduct

by trentHarlem

JavaScript

/* // n = number, i = index, a = array
function computeMax(n, i, a) {
  let max = 0
  if (n * a[i + 1] > max) {
    max = n * a[i + 1]
    console.log('max', max)
    return max
  }
}


function adjacentElementsProduct(array) {
  return array.forEach(computeMax)
} */

//--------------------------------------------



/* function adjacentElementsProduct(array) {
   let max = 0 
  array.forEach((n, i, a) => {
    if (n * a[i + 1] > max) {
      max = n * a[i + 1]
      console.log('max',max)
      return max
    }
  })
} */

//--------------------------------------------

function adjacentElementsProduct(a) {
  let max = -Infinity
  for (let i = 0; i < a.length - 1; i++) {
    console.log(a[i] * a[i + 1])
  console.log('max', max)
    
    if (a[i] * a[i + 1] > max) {
      max = (a[i] * a[i + 1])
    }
  }
  console.log('max', max)
  return max
}

//negative index
/* function adjacentElementsProduct(array) {
  let max = 0
  for (let i = 1; i < array.length; i++)
      console.log(array[i],'*', array[i -1])
  
    if (array[i] * array[i -1] > max) {
      max = (array[i] * array[i -1])
      console.log('max', max)
      return max
    }
} */
//--------------------------------------------




//console.log(adjacentElementsProduct([1, 5]), 6);
console.log(adjacentElementsProduct([1, 5, 10, 9]), 90);
//console.log(adjacentElementsProduct([-23, 4, -5, 99, -27, 329, -2, 7, -921]), -14);

//console.log(adjacentElementsProduct([4, 12, 3, 1, 5]), 48);
//console.log(adjacentElementsProduct([5, 1, 2, 3, 1, 4]), 6);


//--------------------------------------------

/* function arrayDiff2(a, b) {
let array= [...a,...b]
//let obj= {...a,...b}
let obj= {array}
console.log(array)
//console.log(obj)
  //let output = [...new Set(a.concat(b))]
  return output = [...new Set(array)]
  //return Array.from(obj)
}
console.log(arrayDiff2([3,4], [3]), [4], "a was [3,4], b was [3]");
 */
/* obj = {}
//obj require UNIQUE keys
for (let i of a) {
  obj[i] = true
}*/