
- 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
Finding median index of array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array, arr, that represents the positions of different asteroids in a one-dimensional space.
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Our function is supposed to find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
For example, if the input to the function is −
Input
const arr = [1, 7, 3, 6, 5, 6];
Output
const output = 3;
Output Explanation
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.
Example
Following is the code −
const arr = [1, 7, 3, 6, 5, 6]; const medianIndex = (arr = []) => { let sum = arr.reduce((acc, num) => acc + num, 0) let currentSum = 0 for (let i = 0; i < arr.length; i++) { currentSum += (arr[i - 1] || 0) sum -= arr[i] if (currentSum === sum) { return i } } return -1 } console.log(medianIndex(arr));
Output
3
- Related Articles
- Finding the index position of an array inside an array JavaScript
- Finding median for every window in JavaScript
- Finding reversed index of elements in arrays - JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Calculating median of an array in JavaScript
- Calculating median of an array JavaScript
- Finding the index of last element in the array in C#
- Finding the index of first element in the array in C#
- Finding quarter based on month index in JavaScript
- Finding special array - JavaScript
- Find closest index of array in JavaScript
- Finding the rotation of an array in JavaScript
- Finding the mid of an array in JavaScript
- Finding degree of subarray in an array JavaScript
- Finding the 1-based index of a character in alphabets using JavaScript
