1

I'm writing a small webpage that will enable students to answer questions and get feedback on their answers.

Part of this detection checks for common errors to give them guidance. Specifically I want to check if their answer is a power of ten out from the actual answer.

If the answer was 3.93E-6, this condition should activate if they type 3.93E2, 3.93E-9, 3.93 etc.

The obvious way to me to test this is to do something like this:

var correct = 3.93E-6;
var entry = 3.93E-2; //really comes from an input box.

if (!(entry / correct)%10) {
    alert ("power of ten error");
}

However, this doesn't work as error/correct doesn't work for large/small numbers.

How can I fix this?

Live code at: http://bradshawenterprises.com/test.html

1
  • In the end I did a bit of both: correct.toExponential().toString().split('e'); I converted each number to exp, then to a string and split it, then compared the first element of the resulting array. Thanks! Feb 13, 2009 at 10:21

3 Answers 3

2
var num = 12.4123;
var numString = num.toExponential()
// numString = "1.24123e+1"

This normalizes the number, but you have to parse it manually. (Like on how accurate the result has to be…)

2

Here's one way to see if two numbers are off by approximately a power of ten:

var correct = 3.93E-6;
var entry = 3.93E-2;
var epsilon = .01;

var log10_ratio = Math.log(correct/entry)/Math.log(10);
if (Math.abs(Math.round(log10_ratio) - log10_ratio) < epsilon) {
    alert ("power of ten error");
}
2
  • didn't you mean >= epsilon?
    – Christoph
    Feb 13, 2009 at 10:46
  • @Christoph: No, the goal is to to inform the student if their entry is about a power of 10 off from the correct answer. If it's not, it's not a "power of ten error"
    – Miles
    Feb 13, 2009 at 10:51
1

If they're required to enter the answer with the "E" notation, why not just check if all the stuff before the "E" is the same in both the student's answer, and the correct answer.

Of course you might also want to give them an idea of how many decimal places they should keep, otherwise 1.2E5 and 1.21E7 wouldn't trigger the "power of ten error"

1
  • Part of the test is to get the decimal places correct - there is another condition that triggers that. Feb 13, 2009 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.