好的,所以在您提出批评之前,是的,我具有所需的输出:

var interest = prompt("Enter a percentage using a decimal Number...");
var interestRate = interest*100;
var startCash = 10000;
var total = startCash*interestRate/100+startCash;
var month = 1;

console.log("Starting Money: £" + startCash);
console.log("Interest Earned: " + interestRate + "%");
console.log("Total Amount: £" + parseInt(total));
console.log("This is Month: " + month);


关于如何将小数转换为百分比有很多答案,但是几乎没有任何答案可以将该百分比添加到起始数字中。是的,可以肯定,如果我们事先知道百分比很容易,但是我正在尝试根据用户输入进行操作,因此该百分比可以是随机的。

我所追求的效果是,我只是问这是否是正确的方法,或者是否有更短的方法。您可能会告诉我,我对javascript较新。

还有没有办法让他们只能输入十进制数字,以确保代码停留在所需的输出上?

最佳答案

稍短一些:(首先编辑提示解析)

var interest = parseFloat(prompt("Enter a percentage using a decimal Number..."));
var startCash = 10000;
var total = startCash*(interest+1);
var month = 1;

console.log("Starting Money: £" + startCash);
console.log("Interest Earned: " + interest*100 + "%");
console.log("Total Amount: £" + parseInt(total));
console.log("This is Month: " + month);


实际上,考虑到此方法需要额外的解析,我想我也很喜欢您的原始方法。

10-07 16:19