- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为我的学生创建一个动态工作表,这样他们每次创建时都会看到不同的问题。我正在尝试创建的问题,即如果我在总 Y 中得到 X,则计算百分比。
这是一起工作的 3 个函数,第一个生成一些数字,调用第二个,然后调用第三个来检查它是否超过 2 个小数位,如果是,第二个创建一个新的 SCORE 数字重复直到找到小数点后 2 位或更少的答案,然后返回适用于第一个的 SCORE 数字,输出它。
我不断收到以下三种输出之一:未定义 SCORE 应在的位置、根本没有输出或一个工作问题。
有时我无法理解它是如何工作的,有时会抛出 undefined 而在其他时候则完全没有给出。
任何想法。
function scorePercent()
{
var output="";
var total = Math.floor((Math.random()*99)+1);
var score = Math.floor((Math.random()*(total-1))+1);
output = output + "<div>A score of " + chkScore(score,total) + " out of " + total + ".</div></br>";
document.getElementById("qOut").innerHTML=output;
}
function chkScore(n1,n2)
{
var answ = (n1/n2)*100;
if(dps(answ)>2)
{
var scoreNew = Math.floor((Math.random()*(n2-1))+1);
chkScore(scoreNew, n2);
}
else
{
return n1;
}
}
function dps(num)
{
var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) { return 0; }
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0)
// Adjust for scientific notation.
- (match[2] ? +match[2] : 0));
}
最佳答案
您的 chkScore
中有一个递归函数,但您没有返回
-ing 来自“更深层次”迭代的结果。
试试这个:
function chkScore(n1,n2){
var answ = (n1/n2)*100;
if(dps(answ)>2) {
var scoreNew = Math.floor((Math.random()*(n2-1))+1);
return chkScore(scoreNew, n2); // <-- return that.
} else {
return n1;
}
}
那里缺少return
,导致函数有时不返回任何东西。
“更深层次”的迭代只会返回它们的值向上 1 个“级别”,所以“级别”必须通过它,如果你明白我的意思的话。
关于Javascript : function checking if the answer to score as a percentage has more than 2 decimal places,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13931127/
Mockito 中这两个声明有什么区别? @Mock(answer = Answers.CALLS_REAL_METHODS) ArrayList mock; @Spy ArrayList spy;
我认为形成这个问题的最好方法是举一个例子......所以,我决定问这个问题的实际原因是因为 Problem 55 on Project Euler 。在该问题中,要求找到 10,000 以下的 Lyc
我编写了代码来计算 QuickSort 中完成的比较次数。 每当对长度为 m 的数组执行快速排序时,该算法都会将比较次数增加 m-1(因为主元将与除自身以外的所有内容进行比较)。 枢轴的选择始终是数组
菜鸟开发人员,对任何非常规代码表示歉意。 我正在做一个简单的测验。一次显示一个问题,单选按钮上有 3 个可能的答案。答案“key”存储在名为 allQuestions 的数组中。 我正在寻找正确的方法
我在使用 Java 的 Sphinx 语音识别库时遇到问题。我正在使用它来获取输入并处理它。我第一次获得输入时,它起作用了。第二次,它在我有机会说话之前立即回答了自己。在那之后,它只是继续回答自己。我
ruby 问题: 我可以缩短以下代码吗: total = Paper.where(available: true).count puts total if total > 0 我想象过这样的事情,但我
进程文件: answers or answers.exe 进程名称: 1-Click Answers 进程类别:存在安全风险的进程 英文描述: an
这question and its answers这里建议两者: 可以使用正则表达式匹配嵌套模式。 这是不可能的,因为嵌套模式不是常规语言,因此 DFA(正则表达式)无法识别它们。 在阅读上述问题的答
我正在尝试实现“算法简介”一书中所述的合并排序算法。尽管实现是按照书中指定的,但输出不正确。很有可能出现相差一的错误,但我无法指出它。有什么指点吗? #include #include #defi
我在我的应用程序中添加了一个新的首选项,以允许用户选择退出分析报告。我在我的应用程序 Crashlytics 和 Fabric Answers 中使用。 我在 onCreate 中有这段代码: if
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
这里是Python新手用户。 我有一个文件,last.txt,我正在从中提取信息,该信息显示美国最常见的姓氏,以及它们以整数/小数形式出现的百分比频率。我希望能够检索三个字母长的姓氏数量并将其分配给答
禁用 Crashlytics 错误报告相对 straight forward. .我还想禁用调试版本的答案。然而, new Crashlytics.Builder().answers(null); 不
我是游戏新手,所以可能是个简单的问题。我想做的是制作一个应用程序,其中包含一个问题和一个用于回答的文本文件。然后,我希望应用程序能够识别答案何时正确(在本例中为数字 25)以及何时错误(不是 25)。
http://piratepad.net/IY48xXzsys (由 Peter West 修改,原文来自此处:http://paulbutler.org/archives/a-simple-diff
我正在使用 Answers (Fabric) 以便在我的应用程序中进行一些分析,但我在 fabric.io 中的 Answers 仪表板遇到问题。当我收到从我的应用程序发送的事件时,我无法看到自定义属
我有一个使用 Crashlytics with Answers 的应用程序。两者都工作正常,我看到正在跟踪框架中内置的事件。 我正在尝试通过在我的 View Controller 中使用以下代码行来添
我正在为调查系统构建数据库。 We have employees that fill in a survey with categories that have questions. The empl
我是 Excel 2003 中的 VBA 新手。我编写了一个包含两个字符串的 UDF:一个过滤器和一个将作为单元格引用的字符串。当我在 VBA 编辑器中调试并连续调用该函数几次时,一切都按我的预期工作
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3个月前关闭。 Improve th
我是一名优秀的程序员,十分优秀!