
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Maximum product of any two adjacent elements in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers.
Our function should find the maximum product obtained from multiplying 2 adjacent numbers in the array.
Example
Following is the code −
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));
Output
50
- Related Articles
- JavaScript: Adjacent Elements Product Algorithm
- Maximum product of 4 adjacent elements in matrix in C++
- Maximum decreasing adjacent elements in JavaScript
- Maximum sum such that no two elements are adjacent in C++
- Find Two Array Elements Having Maximum Product in Java?
- Maximum sum of difference of adjacent elements in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Program to find maximum product of two distinct elements from an array in Python
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum length product of unique words in JavaScript
- Maximum set bit sum in array without considering adjacent elements in C++

Advertisements