Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the number of 50's and its exist number from number like 230

I have number like 230.
And 50 is always the limit.
I want to get the following data:

[50,50,50,50,30]

I should get four 50’s and its exist number that is less than 50.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

example 2.

Number = 400

I should get
[50,50,50,50,50,50,50,50]

example 3:

Number = 110

I should get

[50,50,10]

>Solution :

getResult(420, 50) = [50, 50, 50, 50, 50, 50, 50, 50, 20]

getResult(51, 50) = [50, 1]

getResult(12, 50) = [12]

This is a basic way of doing it using % which is the modular operator.

console.log(getResult(420, 50));


function getResult(input, num) {
  if (input <= num) return [input];

  arr = [];

  for (let i = 0; i < Math.floor(input / num); i++) {
    arr.push(num);
  }

  let remainder = input % num;

  if (remainder > 0) {
    arr.push(input % num);
  }

  return arr;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading