这是我想做的一个例子..。
假设我有73284个号码。
千的数量为73 (73 284除以1 000)。
数百的数目为2 (284除以100)。
tens为8 (84除以10)。
单打的数量是4 (4是左)。
我需要的是一个Javascript函数,它将接受数字73,284,并使用上面的标准从它创建4个数字。
所以如果这个数字是73,284,我会把这个数字作为参数传递到函数中,这个函数会返回一个数组,看起来是这样的,73,2,8,4。
我尝试使用Math.round()函数。它似乎适用于数千人,但不一定适用于数百人、数十人和单身人士。
发布于 2022-11-18 20:49:43
你可以通过跟踪你拥有的数千、数百和几十个,并在你去的时候移除这些来做到这一点。一个简单的算术。
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);
发布于 2022-11-18 20:52:54
简单的循环。
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));
发布于 2022-11-18 21:07:37
您需要将除法与Math.floor和模运算符(%)结合起来。Modulo操作符返回一个除法的提醒。
由于您将多次执行此操作,因此这是使用递归的好机会。这是我想出来的。
首先,我们需要知道在我们的投入中有多少个给定的偏离者。要做到这一点,可以使用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).那么,我们需要检查是什么提醒了那个操作,这就是模操作符出现的地方。
const input = 73284
const divider = 1000
const reminderOfTheDivision = input % divider;
// this gives us 284.然后,我们可以使用这个提醒作为我们的新输入,除以除以10,得到多少包含在其中的数百个。
// 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)直到我们降到个位数为止。
如您所见,这段代码中有很多重复的地方。一个好的开发人员正在不惜一切代价避免重复。为了防止这种情况,我们可以使用循环或递归函数。递归函数基本上是一个调用自己的函数。我选择使用函数,因为它更干净。
这是我想出来的。
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)
https://stackoverflow.com/questions/74495056
复制相似问题