找到获得的价值我哪里错了?
finding value gained where am I going wrong?
我正在尝试编写一段简单的脚本来告诉我从两个值之间的百分比差异中获得的数量。这是我拥有的,但 return 数量不正确。
function percentUP (money,newNum,Orignal){
var increase = newNum - Orignal;
var percent = Math.floor(Math.round(increase/Orignal*100));
var gains = Math.round((money/percent)*100);
return "you would make £" + gains + " from your " + "£" + money + " investment"
};
我一直在用 (10,30,10) 增加 200% 的简单函数对其进行测试,结果应该是:
“您将从 10 英镑的投资中赚取 30 英镑”
但我得到:
“您将从 10 英镑的投资中赚取 5 英镑”
对不起,如果这真的很明显,我现在正在学习。
您 gains
计算有误。你的 percent
在你的例子中等于 200。你想用它乘以钱再除以 100。
你的计算:money / 200 * 100 = money / 2
预期计算:money * 200 / 100 = money * 2
更正后的代码:
function percentUP(money, newNum, orignal) {
var increase = newNum - orignal;
var percent = Math.round(increase / orignal);
var gains = Math.round(money * percent);
return `you would make £${gains} from your £${money} investment`
};
PS 您还可以从 percent
计算中删除 Math.round
调用。它只会打乱准确性。
收益计算好像有误。它应该是 Original +(Original * Percent / 100)。那应该是 10 + (10 * 200/100)。您也可以在上面的示例中使用您的 money 变量而不是 Original。根据您的计算结果,我不确定两者之间的差异。
此外,不确定百分比形式的目的是什么,只使用小数形式更简单,即 2 而不是 200%。这样你就可以避免两次转换。
我正在尝试编写一段简单的脚本来告诉我从两个值之间的百分比差异中获得的数量。这是我拥有的,但 return 数量不正确。
function percentUP (money,newNum,Orignal){
var increase = newNum - Orignal;
var percent = Math.floor(Math.round(increase/Orignal*100));
var gains = Math.round((money/percent)*100);
return "you would make £" + gains + " from your " + "£" + money + " investment"
};
我一直在用 (10,30,10) 增加 200% 的简单函数对其进行测试,结果应该是:
“您将从 10 英镑的投资中赚取 30 英镑”
但我得到: “您将从 10 英镑的投资中赚取 5 英镑”
对不起,如果这真的很明显,我现在正在学习。
您 gains
计算有误。你的 percent
在你的例子中等于 200。你想用它乘以钱再除以 100。
你的计算:money / 200 * 100 = money / 2
预期计算:money * 200 / 100 = money * 2
更正后的代码:
function percentUP(money, newNum, orignal) {
var increase = newNum - orignal;
var percent = Math.round(increase / orignal);
var gains = Math.round(money * percent);
return `you would make £${gains} from your £${money} investment`
};
PS 您还可以从 percent
计算中删除 Math.round
调用。它只会打乱准确性。
收益计算好像有误。它应该是 Original +(Original * Percent / 100)。那应该是 10 + (10 * 200/100)。您也可以在上面的示例中使用您的 money 变量而不是 Original。根据您的计算结果,我不确定两者之间的差异。
此外,不确定百分比形式的目的是什么,只使用小数形式更简单,即 2 而不是 200%。这样你就可以避免两次转换。