How to divide sum into equal parts with last one being remainder(if necessary)? JAVASCRIPT | Sololearn: Learn to code for FREE!

+1

How to divide sum into equal parts with last one being remainder(if necessary)? JAVASCRIPT

Example: var a = sum; // 479,25 var b = divider number; // 6 So that programs result would display: 79,88 79,88 79,88 79,88 79,88 79,85 It could also display: 80 80 80 80 80 79,25 Both is right, as long as the sum of divided parts is still equal to 479,25.

4/17/2021 1:09:12 PM

F.M.

8 Answers

New Answer

+1

Hi F.M. I see you have posted 3 questions over this topic. Just curious as to why, I saw people answering your question, so, you gotta have a good reason for reposting this topic, I guess.

+1

Hi Ipang, I deleted one post now. I posted them because, so far, I didn’t get the solution. Do you have it?

+1

Well, I kinda do, But you only have 1 test case (479.25 divided by 6) I don't know how my idea will work when a different number is given to test. Anyways, you got a code this far? (Edit) This is my idea for code https://code.sololearn.com/W5yhd83PtLgE/?ref=app

+1

I don’t.. Here is another example: var a = 97.82; // total bill var b = 4; // no. of installments var c = a/b; /* which is 97,82/4 = 24,455 - with .toFixed(2) it’s 24,46 */ But now, the sum of 4 installments = 97.84. So I need different way, to divide installments into 4 amounts. The amounts could also display 3 same amounts with the last one being the remainder of the total sum(97.82).

+1

I think, I understand the task now. Here's an attempt (though not js), see my comments in the script. https://code.sololearn.com/ccMFyMUw7xvf/?ref=app

+1

Take remainder of number. Then subtract remainder from original number . Divide the number print the divided number for required times and at last add the remainder to the last one . Suppose your number is 29 and you have to divide it into 3 parts . var sum = 29; var divisor =3; var remainder =sum%divisor; var numbers = (sum-remainder)/3; for(var i=0;i<divisor+1;i++){ if(i==3){ console.log(numbers+remainder); } else{ console.log(numbers); } }

0

Hi divyanshu kumar . Your code is in the right direction. But it shows me only one number. In this case the result shows: 11 But, what I would like show is: 9 9 11 How to do that?

0

Hii F.M. I just missed some closing braces re run I have edited the program now it should work .... if you like the code don't forget to upvote...