JavaScript 中任意两个相邻元素的最大乘积
问题
我们需要编写一个 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP