[英]My function will always tell me that I got my question wrong. Why?
I made a function that will quizzes you on Pi. 我做了一个函数,将在Pi上测验你。 For every digit you write correctly, you get half of a point.
对于你正确编写的每一个数字,你得到一半的分数。 However, even if I just type '3', it'll say that I did it incorrectly.
但是,即使我只输入'3',也会说我做错了。 Please help.
请帮忙。
function pi() { var piWithoutDecimals = 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679; var yourAnswer = prompt("Pi Bonus! Half of a point for every digit of pi that you can write from memory!"); var yourAnswerNum = parseInt(yourAnswer); var yourAnswerMultiplier = yourAnswerNum.toString().split("."); var yourAdjustedAnswer = yourAnswerNum*(10**parseInt(yourAnswerMultiplier)); var yourAdjustedAnswerCount = yourAdjustedAnswer.toString().length; var yourSectionOfpiWithoutDecimals = piWithoutDecimals.toString().substring(0,parseInt(yourAdjustedAnswer-1)); if(yourSectionOfpiWithoutDecimals==yourAnswerNum) { alert("Nice!! You listed "+yourAdjustedAnswerCount+" digits of Pi, and so you get "+Math.floor((yourAdjustedAnswerCount)/2)+" points!") points+=Math.floor((yourAdjustedAnswerCount)/2); } else { alert("Good try, but you got one or more digits incorrect.") } } pi()
You might have a simple approach. 你可能有一个简单的方法。
.
.
, and count length to get number of correctly entered digits. function pi() { const pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"; const yourAnswer = prompt("Pi Bonus! Half of a point for every digit of pi that you can write from memory!"); if(pi.startsWith(yourAnswer)) { const yourAdjustedAnswerCount = yourAnswer.replace('.', '').length; alert(`Nice!! You listed ${yourAdjustedAnswerCount} digits of Pi, and so you get ${Math.floor(yourAdjustedAnswerCount / 2)} points!`) } else { alert("Good try, but you got one or more digits incorrect.") } } pi()
You first go wrong at this line: 你第一次出错了:
var yourAnswerMultiplier = yourAnswerNum.toString().split(".");
If you log this value for eg the input "314"
, you'll get the array ["314"]
(your probably wanted .split("")
). 如果您将此值记录为例如输入
"314"
,您将获得数组["314"]
(您可能需要.split("")
)。 Going from there, yourAdjustedAnswer
will be Infinity (since 10 ** 314 is too large and simply overflows). 从那里开始,你的
yourAdjustedAnswer
将是Infinity(因为10 ** 314太大而且只是溢出)。 This fails which is obviously not what you wanted. 这失败了,这显然不是你想要的。
As a suggestion, try to step through the execution for a simple example using the developer tools of your browser/IDE and watch closely if each step is according to your expectation. 作为建议,尝试使用浏览器/ IDE的开发人员工具逐步执行一个简单的示例,并仔细观察每个步骤是否符合您的期望。
@Orkhan Alikhanov's answer might also provide some hints. @Orkhan Alikhanov的回答也可能提供一些提示。
function pi() { var piWithoutDecimals = 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679; var yourAnswer = prompt("Pi Bonus! Half of a point for every digit of pi that you can write from memory!"); var yourAnswerNum = parseInt(yourAnswer); var yourAnswerMultiplier = yourAnswerNum.toString().split("."); var yourAdjustedAnswer = yourAnswerNum*(10**parseInt(yourAnswerMultiplier)); console.log(yourAdjustedAnswer) var yourAdjustedAnswerCount = yourAdjustedAnswer.toString().length; var yourSectionOfpiWithoutDecimals = piWithoutDecimals.toString().substring(0,parseInt(yourAdjustedAnswer-1)); if(yourSectionOfpiWithoutDecimals==yourAnswerNum) { alert("Nice!! You listed "+yourAdjustedAnswerCount+" digits of Pi, and so you get "+Math.floor((yourAdjustedAnswerCount)/2)+" points!") points+=Math.floor((yourAdjustedAnswerCount)/2); } else { alert("Good try, but you got one or more digits incorrect.") } } pi()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.