Answered by:
How to create array from whole numbers in Javascript

Question
-
User1406973109 posted
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
Tuesday, June 11, 2019 1:31 PM
Answers
-
User665608656 posted
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"> <head runat="server"> <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> </head> <body> <form id="form1" runat="server"> <div> num:<asp:TextBox ID="TextBox1" runat="server" TextMode="Number" Width="80px"></asp:TextBox> parts:<asp:TextBox ID="TextBox2" runat="server" TextMode="Number" Width="80px"></asp:TextBox> <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.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 12, 2019 2:11 AM
All replies
-
User665608656 posted
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"> <head runat="server"> <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> </head> <body> <form id="form1" runat="server"> <div> num:<asp:TextBox ID="TextBox1" runat="server" TextMode="Number" Width="80px"></asp:TextBox> parts:<asp:TextBox ID="TextBox2" runat="server" TextMode="Number" Width="80px"></asp:TextBox> <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.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 12, 2019 2:11 AM -
User1406973109 posted
Thanks Yongqing Yu, you have solved the problem.
Thanks a lot.
I Appreciate
Wednesday, June 12, 2019 12:02 PM