获取 JavaScript 的结果数字。

huangapple go评论54阅读模式
英文:

Get the resulting number with js

问题

基本上,我的问题是,我试图弄清楚如何根据以下逻辑使用JS计算并返回一个数字。

例如:我有一个数字120,我想检查这个数字是否大于100(是的),好的,这个数字是否小于200(是的),然后返回(1 * 300)。

例如:另一个数字为230,这个数字是否大于200(是的),好的,这个数字是否小于300(是的),然后返回(2 * 300)。

以此类推。

我已经通过使用switch语句实现了这个功能,但我认为这样做太奇怪了,而且对于像10000、20000这样的大数字来说,需要大量的switch语句或if条件。

我希望你明白我的意思。

英文:

So basically my problem is that am trying to figure out how to calculate and return a number with JS according to the following logic.

Ex: i have a number of 120 i want to check well is the number greater than 100 ( yes ) okay is the number less than 200 ( yes ) then return ( 1 * 300 )

Ex : another number as 230 is the number greater than 200 ( yes ) okay is the number less than 300 ( yes ) then return ( 2 * 300 )

and sooo on.

i made it through switch case but it think it is too odd to do this and it is possible to deal with large numbers like 10000 , 20000 so i will need endless switch cases or if conditions

i hope you got my point

答案1

得分: 1

根据上面的注释,不考虑边缘情况(例如负数),您可以使用以下代码:

Math.floor(n / 100) * 300
英文:

As per the comment above, not considering edge cases (negative numbers for example), you might go with:

Math.floor(n / 100) * 300

huangapple
  • 本文由 发表于 2023年6月12日 14:43:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定