在JavaScript中计算样本测试分数

时间:2018-01-22 15:45:36

标签: javascript

我被问到以下问题,并提出了一个解决方案,但我似乎无法弄清楚其他条件是如何得到满足或满足的。

function scoreTest(correct, questions) {
var percent;
// Only change code below this line
percent = (correct*questions); // My logic


// Only change code above this line
return percent;
}

// Change the inputs below to test your code
scoreTest(18,20);

scoreTest(20,25) should return a number //Output met
scoreTest(47,50) should return a value of 94
scoreTest(16,20) should return a value of 80
scoreTest(8,10) should return a value of 80 //Output met
scoreTest(97,100) should return a value of 97
scoreTest(1,50) should return a value of 2

1 个答案:

答案 0 :(得分:3)

你是如此亲密。百分比的计算方式与此(numberCorrect / TotalQuestions) * 100相同。这将为您提供百分比值。

鉴于此,重新计算您的函数以输出百分比:



function scoreTest(correct, questions) {
var percent;
// Only change code below this line
percent = (correct/questions) * 100; // Actual percentage value


// Only change code above this line
return percent;
}

// Change the inputs below to test your code
scoreTest(18,20);

scoreTest(20,25) //should return a number //Output met
scoreTest(47,50) //should return a value of 94
scoreTest(16,20) //should return a value of 80
scoreTest(8,10) //should return a value of 80 //Output met
scoreTest(97,100) //should return a value of 97
scoreTest(1,50) //should return a value of 2