ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน 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