 ## 2 replies

Last post Jun 12, 2019 12:02 PM by timotech

• ### How to create array from whole numbers in Javascript

Good day all,

Please I have an assignment in JavaScript, to get an array from an integer.

E.g. when I have integer num = 10 and I supply another integer parts = 5 to do a perfect even division , I want to get an array of [2,2,2,2,2]

also if I have num = 20 and I have to divide it by parts = 6, I should get this array [3,3,3,3,4,4]

This is sample code below:

```var splitInteger = function(num, parts) {
// Complete this function
var partsArray = [];
if(num > parts)
{
for(i = 1; i <= parts; i++ )
{
var numDivided = num%parts === 0;
partsArray[i].push(numDivided);
}
}
}```

Please how can I do it.

Thanks

Tim

• ### Re: How to create array from whole numbers in Javascript

Jun 12, 2019 02:11 AM|Yongqing Yu|LINK

Hi timotech,

According to your requirements, we first need to find out the relationship between the array and num, parts.

Based on the two examples you provided, we can see that the number of the first part of the array is the integer part of num/parts, and the number of the latter part is the integer part of  num/parts + 1.

The length of the array is equal to parts, and the number of occurrences of the latter part of the array is equal to the value of  num%parts, while the number of occurrences of the former part is parts minus the number of occurrences of  the latter part.

In the JS code, when num > parts, we use var first = parseInt (num / parts) to get the number of the former part.

Then use var y = num%parts to get the number of occurrences of the latter part, and var x = parts-y to get the number of occurrences of the former part.

Next, according to the number of occurrences of the former part  and the number of occurrences of the latter part are looped respectively, and put the corresponding values in the loop into the array.

For more details, you could refer to the following code:

```<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<title></title>
<script type="text/javascript">
function PutArray() {
document.getElementById("Label1").innerHTML = "";
var num = parseInt(document.getElementById("TextBox1").value);
var parts = parseInt(document.getElementById("TextBox2").value);
if (num < 0 || parts < 0) {
document.getElementById("Label1").innerHTML = "Please enter non-negative integers!";
return;
}
var partsArray = [];
if (num > parts) {
var first = parseInt(num / parts); //The first part of the number
var y = num % parts;//Number of occurrences of the latter part
var x = parts - y;//Number of occurrences of the previous part

for (var i = 0; i < x; i++) {
partsArray.push(first);
}
for (var i = 0; i < y; i++) {
partsArray.push(first + 1);
}

for (var i = 0; i < partsArray.length; i++) {
if (i == partsArray.length - 1) {
document.getElementById("Label1").innerHTML += partsArray[i];
} else {
document.getElementById("Label1").innerHTML += partsArray[i] + ",";
}
}
} else {
document.getElementById("Label1").innerHTML = "parts should be smaller than num!";
}
}

</script>
<body>
<form id="form1" runat="server">
<div>
num:<asp:TextBox ID="TextBox1" runat="server" TextMode="Number" Width="80px"></asp:TextBox>&nbsp;&nbsp;
parts:<asp:TextBox ID="TextBox2" runat="server" TextMode="Number" Width="80px"></asp:TextBox>&nbsp;&nbsp;
<input id="Button1" type="button" value="button" onclick="PutArray()" /><br />
<br />
arrray:<asp:Label ID="Label1" runat="server" Text="" ForeColor="#ff0000"></asp:Label>

</div>
</form>
</body>
</html>
```

The result of my work demo: Best Regards,

YongQing.

MSDN Community Support