
- 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
Code to find the center of an array without using ES6 functions - JavaScript
We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops.
If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements.
Example
Following is the code −
const arr = [14, 32, 36, 42, 45, 66, 87]; const array = [13, 92, 83, 74, 55, 46, 74, 82]; const midElement = (arr, ind = 0) => { if(arr[ind]){ return midElement(arr, ++ind); }; return ind % 2 !== 0 ? [arr[(ind-1) / 2]] : [arr[(ind/2)-1], arr[ind/2]]; }; console.log(midElement(arr)); console.log(midElement(array));
Output
This will produce the following output in console −
[ 42 ] [ 74, 55 ]
- Related Articles
- Flattening an array with truthy/ falsy values without using library functions - JavaScript
- C++ code to find center of inner box
- How to clone an array in ES6?
- JavaScript code to print last element of an array
- JavaScript: How to Find Min/Max Values Without Math Functions?
- Finding square root of a number without using library functions - JavaScript
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
- Find the middle element of an array using recursion JavaScript
- Reverse digits of an integer in JavaScript without using array or string methods
- What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
- Sorting Array without using sort() in JavaScript
- Finding alphabet from ASCII value without using library functions in JavaScript
- Find maximum in an array without using Relational Operators in C++
- Find minimum in an array without using Relational Operators in C++
- Find the difference of largest and the smallest number in an array without sorting it in JavaScript

Advertisements