Here's an example of what I'm trying to do...
Let's say I have the number 73,284.
The number of thousands is 73 (73,284 divided by 1,000).
The number of hundreds is 2 (284 divided by 100).
The number of tens is 8 (84 divided by 10).
The number of singles is 4 (4 is left).
What I need is a Javascript function that would take the number 73,284 and create 4 numbers from it using the criteria above.
So if the number was 73,284, I'd pass that number into the function as a parameter and the function would return an array that looks like this, [73,2,8,4].
I tried to use the Math.round() function. It seemed to work for the thousands, but not necessarily for the hundreds, tens, and singles.
CodePudding user response:
Simple for loop.
const f = (n) => {
let div = 1000;
const result = [];
for (let i = 0; i < 4 && n; i , n %= div, div /= 10) {
result.push(Math.floor(n / div));
}
return result;
}
console.log(f(73284));
CodePudding user response:
You can do this by keeping track of how many thousands, hundreds and tens you have and removing these as you go. A little simple arithmetic.
function get(num){
const thousands = Math.floor(num/1000)
const hundreds = Math.floor((num-thousands*1000)/100);
const tens = Math.floor((num-thousands*1000-hundreds*100)/10);
const units = Math.floor(num-thousands*1000-hundreds*100-tens*10)
return [
thousands,
hundreds,
tens,
units
]
}
const [th,hu,te,un] = get(73284)
console.log("Thousands=",th);
console.log("Hundreds=",hu);
console.log("Tens=",te);
console.log("Units=",un);
CodePudding user response:
You will need a combinaison of division with Math.floor
and modulo operator (%
). The Modulo operator returns the reminder of a division.
Since you will be doing this operation multiple times, this a great opportunity to use recursion. Here is what i've come up with.
First, we need to get how many of a given devider we've got in our input. To do that, you can devide it and ignore the decimal using Math.floor
.
const input = 73284
const divider = 1000
const amoutOfDividerInInput = input / divider .
// this returns 73.284, we don't want the .284 so we can use Math.floor.
const amountOfDividerInInputWithoutDecimal = Math.floor(amoutOfDividerInInput)
// we print the value
console.log("there are " amountOfDividerInInputWithoutDecimal " 1000s in " input).
We, then, need to check what is the reminder of that operation, this is where the modulo operator comes in.
const input = 73284
const divider = 1000
const reminderOfTheDivision = input % divider;
// this gives us 284.
We can, then, use this reminder as our new input, and divide the divider by 10, to get how many hundreds are contains in it.
// we devide the divider by 10,
const newDevider = divider / 10;
// we calculate the new amount and reminder
const amoutOfNewDividerInInput = Math.floor(reminderOfTheDivision / newDivider );
const reminderOfTheNewDivision = reminderOfTheDivision % newDivider
// we print the new values
console.log("There are " amoutOfNewDividerInInput " 100s in" input)
And this goes on until we are down to the single digit.
As you can see, there are alot of repetition in this code. A good developer is trying to avoid repetition at all cost. To prevent this, we could use a loop or a recursive function. A recursive function is basically a function that call itself. I've choose to use a function, since it's cleaner.
Here is what i've come up with.
function findDividingQuantity(reminder, divider) {
// if the devider is less than 10, we simply print the reminder.
if(divider < 10) {
console.log('and ' reminder ' remains.');
// and we returns to stop the recursion.
return;
}
// otherwise, we get the current amount of the divider in the reminder.
const amount = Math.floor(reminder / divider);
// we then calculate how much is left after the first division,
// this will become our new reminder.
const newReminder = reminder % divider;
// knowing how many of the divider there is in the input, we can print it.
console.log(amount " " divider "s.");
// we call the function again, but with the new reminder
// and the divider divided by 10
findDividingQuantity(newReminder, divider / 10)
}
const input = 73284
console.log("In " input " there is: ");
// we call the function with the initial divider: 1000.
findDividingQuantity(input, 1000)