ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่รับอาร์เรย์ของตัวเลข ฟังก์ชันควรหาผลคูณสูงสุดที่ได้จากการคูณตัวเลข 2 ตัวที่ติดกันในอาร์เรย์
ตัวอย่าง
โค้ดตัวอย่างดังนี้:
const arr = [9, 5, 10, 2, 24, -1, -48];
function adjacentElementsProduct(array) {
let maxProduct = array[0] * array[1];
for (let i = 1; i < array.length; i++) {
product = array[i] * array[i + 1];
if (product > maxProduct)
maxProduct = product;
}
return maxProduct;
}
console.log(adjacentElementsProduct(arr));
ผลลัพธ์
50