
- 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
Algorithm to dynamically populate JavaScript array with zeros before and after values
We are given a months array, which elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements, if the element is present in the original array we use that element otherwise we use at that place.
For example −
Intput → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 10, 0, 0]
Now, let’s write the code −
Example
const months = [6, 7, 10, 12]; const completeMonths = (arr) => { const completed = []; for(let i = 1; i <= 12; i++){ if(arr.includes(i)){ completed.push(i); }else{ completed.push(0); } }; return completed; }; console.log(completeMonths(months));
We iterated from 1 to 12, kept checking if the original array contains the current element, if yes then we pushed that element to the new array otherwise we pushed 0 to the new array.
Output
The output in the console for the above code will be −
[ 0, 0, 0, 0, 0, 6, 7, 0, 0, 10, 0, 12 ]
- Related Articles
- How to set JavaScript object values dynamically?
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- How to populate a 2d array with random alphabetic values from a range in Java?
- How to declare Java array with array size dynamically?
- In-place Algorithm to Move Zeros to End of List in JavaScript
- How to preview an image before and after upload in HTML and JavaScript?
- How to create and populate two-dimension Java array?
- Dynamically creating keys in JavaScript associative array
- Remove leading zeros in array - JavaScript
- How to create form dynamically with the JavaScript
- How to create and populate Java array of Hash tables?
- How to add zeros before numbers in R?
- Looping numbers with object values and push output to an array - JavaScript?
- JavaScript reduce sum array with undefined values
- Remove leading zeros in a JavaScript array?

Advertisements