Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This used to be university work now I put it our here for review purposes.

  1. Could this be constructed better?
  2. Is there any easier path, performance-wise?
  3. Could this be optimised?
  4. Which features and functionalities could/should be added to this game?

<!doctype html> 
<html>

<head>
    <title>Math.floor</title>
</head>

<body>

    <h1> Math.floor: The Maths Game</h1>

    <script>
    // Universty work
    // @author Darik
    // Math.floor


        var score = 0;
        var times = 0;



        function questions(e) {
            var x = Math.floor(Math.random() * 11);
            var y = Math.floor(Math.random() * 11);
            var z = 0;
            var sign = '';

            if (e == 1) {
                sign = '+'
                z = x + y;
                interact(x, sign, y, z)
            } else if (e == 2) {
                sign = '-'
                z = x - y;
                interact(x, sign, y, z)
            } else if (e == 3) {
                sign = '/'
                z = x / y;
                interact(x, sign, y, z)
            } else {
                sign = '*'
                z = x * y;
                interact(x, sign, y, z)
            }

        }

        function interact(x, sign, y, z) {
            times++;
            var userAnswer = prompt("What is " + x + " " + sign + ' ' + y + "?");
            if (z == userAnswer) {
                document.write("<p>" + x + " " + sign + ' ' + y + " = " + userAnswer + " is correct </p>");
                score = score + 1;
            } else {
                document.write("<p>" + x + " " + sign + ' ' + y + " = " + userAnswer + " <span> is INCORRECT. The correct answer is </span> " + z + "</p>");
            }
        }

        function _score() {
            if (times == score) {
                document.write('<p> A perfect score </p>');
                document.body.style.backgroundColor = "#FF99CC";
            } else {
                document.body.style.backgroundColor = "#CCFFFF";
            }
            document.write('<p> You have answerd ' + score + ' questions out of ' + times + '</p>');




        }


        questions(2);
        questions(1);
        questions(4);
        questions(3);
        _score()
    </script>
</body>

</html>

share|improve this question

3 Answers 3

You ask some unreasonable division questions.

What is 9 / 0?

That is answerable by the special string Infinity, but not infinity.

What is 0 / 0?

The expected answer is NaN, but responding with NaN will not work.

What is 10 / 3?

10 / 3 = 3.33333333 is INCORRECT. The correct answer is 3.3333333333333335

share|improve this answer
  • Don't put your Java Script code into the HTML document, always use separate files.

  • Don't create global variable. Either put all your code into a "namespace" object, or at least wrap it in a immediately involked function.

  • The parameter e in questions(e) is a terrible choice for multiple reasons:

    • The name itself is far too short and gives absolute no information what it's for.
    • You utilize "magic numbers" that have absolute no connection to its use. What does the number 1 have to do with addition?
  • NEVER EVER, EVER use document.write (as long as you don't know what it does). You are very, very lucky that your code actually works, because with document.write used like you are doing it here, you are overwriting the whole document including the JavaScript code itself and code that doesn't exist can't be executed.

share|improve this answer
        if (e == 1) {
            sign = '+'
            z = x + y;
            interact(x, sign, y, z)
        } else if (e == 2) {
            sign = '-'
            z = x - y;
            interact(x, sign, y, z)
        } else if (e == 3) {
            sign = '/'
            z = x / y;
            interact(x, sign, y, z)
        } else {
            sign = '*'
            z = x * y;
            interact(x, sign, y, z)
        }

You could use a "switch" here to eliminate the multiple ifs, but I think a better item to pass it would be an object consisting of the addition function and the proper sign string.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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