简体   繁体   English

我的功能总是告诉我,我的问题出错了。 为什么?

[英]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. 你可能有一个简单的方法。

  1. Define your pi in a string 在字符串中定义pi
  2. Take user input 接受用户输入
  3. Check if your pi string starts with what user have input 检查你的pi字符串是否以用户输入的内容开头
  4. Remove decimal point . 删除小数点. , and count length to get number of correctly entered digits. ,并计算长度以获得正确输入的数字的数量。
  5. Display output of your quiz 显示测验的输出

 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.

相关问题 为什么我的Web浏览器上的debugg系统告诉我(drawImage不是函数)? - Why does my debugg system on the webbrowser tell me (drawImage is not a function)? 当我将 animation 效果包装在 function 中时,它停止工作。 谁能告诉我为什么? - When I wrap my animation effect in a function, it stops working. Can anyone tell me why? 我想通过滚动来调整图像大小; 但是,我的 function 似乎是错误的。 如果条件正确,我无法使用 - I want to resize image by scrolling; however, my function seems wrong. I can't use if condition properly 我的Jquery按钮-禁用/启用功能不起作用。 谁能告诉我怎么了? - My Jquery button- disable/enable function is not working. Can anyone tell me what's wrong? 我一直把这个问题弄错了。 使用 javascript 计数位 - I keep getting this question wrong. Counting Bits using javascript 谁能告诉我我的代码有什么问题? - Can anyone tell me what is wrong with my code? 谁能告诉我我的代码有什么问题 - Can anyone tell me what is wrong in my code 有人请告诉我$ .parseJSON有什么问题[javascript / jquery] - Someone please tell me what is wrong with my $.parseJSON [javascript / jquery] 我的Angular控制器功能中未定义“已认证”。 我不确定自己在做什么错。 用尽所有选项。 - “Authenticated” is not defined in my controller function in Angular. I am not sure what I am doing wrong. Running out of all the options. 谁能告诉我为什么 try 块中的 setTimeout function 不起作用? - Could anyone tell me why my setTimeout function within the try block does not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM