

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Split number into n length array - JavaScript
We are required to write a JavaScript function that takes in two numbers say m and n, and returns an array of size n with all the elements of the resulting array adding up to m.
Let’s write the code for this function −
Example
Following is the code −
const len = 8; const sum = 5; const splitNumber = (len, sum) => { const res = []; for(let i = 0; i < len; i++){ res.push(sum / len); }; return res; }; console.log(splitNumber(len, sum));
Output
The output in the console: −
[ 0.625, 0.625, 0.625, 0.625, 0.625, 0.625, 0.625, 0.625 ]
- Related Questions & Answers
- Split Array of items into N Arrays in JavaScript
- Splitting Number into k length array in JavaScript
- Number Split into individual digits in JavaScript
- Split one-dimensional array into two-dimensional array JavaScript
- How to split JavaScript Number into individual digits?
- Split number into 4 random numbers in JavaScript
- Can split array into consecutive subsequences in JavaScript
- Split string into groups - JavaScript
- Split tuple into groups of n in Python
- Split Array by part base on N count in JavaScript
- Split string into equal parts JavaScript
- How to split sentence into blocks of fixed length without breaking words in JavaScript
- Split Array into Consecutive Subsequences in C++
- How to split a factor variable into n number of variables equal to factor size with full length in R data frame?
- JavaScript Array length property
Advertisements