<SCRIPT>
function testQuestion(question) {
var answer = eval(question);
// evaluates the string &
returns 10
var output = "What is " + question +
"?"; //creates the
question string
var correct = '<IMG SRC="correct.gif">';
// "correct" gif for the
answer is correct
var incorrec = '<IMG SRC="incorrec.gif">';
// "incorrec" gif for the
answer is wrong
var response = prompt(output,
"0"); // ask the question
// return
the "correct" gif if the person gets the answer right else
// return
the "incorrec" gif if the person gets the answer wrong
return (response == answer) ?
correct : incorrec;
}
</SCRIPT>
<BODY>
<SCRIPT>
// call the function testQuestion()
and pass the string argument "10 + 10"
// and return the "result" back to variable result
var result = testQuestion("10
+ 10");
|__________
|
document.write(result); //
output the results - either the correct.gif or incorrec.gif
</SCRIPT>
Steps:
- function testQuestion() is in
memory for use
- call testQuestion() and pass
the string argument "10 + 10" & return the "result"
back to the variable result
- Write out the "result" to the page
|