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.
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;
}