- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Repeating only even numbers inside an array in JavaScript
We are required to write a JavaScript function that should repeat the even number inside the same array.
For example, given the following array −
const arr = [1, 2, 5, 6, 8];
Output
We should get the output −
const output = [1, 2, 2, 5, 6, 6, 8, 8];
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => { let end = arr.length -1; for(let i = end; i > 0; i--){ if(arr[i] % 2 === 0){ arr.splice(i, 0, arr[i]); }; }; return arr; }; console.log(repeatEvenNumbers(arr));
Output
The output in the console will be −
[ 1, 2, 2, 5, 6, 6, 8, 8 ]
- Related Articles
- Finding even length numbers from an array in JavaScript
- How to create a function which returns only even numbers in JavaScript array?
- Adding only odd or even numbers JavaScript
- Repeat even number inside the same array - JavaScript
- Write a number array and using for loop add only even numbers in javascript?
- Returning an array containing last n even numbers from input array in JavaScript
- Go through an array and sum only numbers JavaScript
- Odd even sort in an array - JavaScript
- C++ code to decrease even numbers in an array
- Finding the largest non-repeating number in an array in JavaScript
- JavaScript construct an array with elements repeating from a string
- How to pull even numbers from an array in MongoDB?
- Finding the only even or the only odd number in a string of space separated numbers in JavaScript
- Finding the index position of an array inside an array JavaScript
- Sum of all the non-repeating elements of an array JavaScript

Advertisements