4

Hi I am very new to Javascript. In the code below, when I test I don't see any output. Can anybody tell me what is wrong?

    <script>
        function myFunction()
        {
            var x="";
            var score=document.myscore.score.value;

            if (score>30)
            {
                x="Expert";
            }
            else
            }
                x="Novice";
            }
            document.write(x);
        }
    </script>
</head>
<body>
    <form name="myscore">
        Score: <id="score"> <input type="number" name= "score">
        <input type="submit" onClick="myFunction()" value="submit">

    </form>


</body>

4
  • 2
    The bracket after else is into the wrong direction. Ask your console! She will tell you... Commented Nov 19, 2017 at 10:40
  • } Close -> { Open Commented Nov 19, 2017 at 10:40
  • I'm voting to close this question as off-topic because it's a small typo. Commented Nov 19, 2017 at 11:00
  • Thanks, I am not using a console as I am simulating the situation of my students in a lesson. (I've started teaching computer science again after a long hiatus teaching other subjects.). Obviously my skills in spotting syntax errors need work. Commented Nov 19, 2017 at 18:18

5 Answers 5

1
<script>
function myFunction()
{
var x="";
var        score=document.myscore.score.value;

if (score>30)
        {
            x="Expert";
        }
        else
        {
            x="Novice";
        }
        document.write(x);
    }
</script>
</head>
<body>
<form name="myscore">
Score: <id="score"> <input type="number" name= "score">
<input type="submit" onClick="myFunction()" value="submit">

</form>


</body>

This should work! The problem was the orientatiom of { after the else.

Sign up to request clarification or add additional context in comments.

Comments

1

A few things:

  1. <id="score"> <input type="number" name= "score"> is probably not what you mean. Maybe you mean <input type="number" name="score" id="score">.
  2. document.write is a no-no, please don't ever use it. It allows you to modify the document "here and now", i.e. while it's loading. Use document.body.innerHTML += x, for example.
  3. var score = document.getElementByID('score').value
  4. The bracket after the else clause

Comments

0
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>enter code here
 <script>
        function myFunction()
        {
            var x="";
            var score=document.myscore.score.value;

            if (score>30)
            {
                x="Expert";
            }
            else
             {
                x="Novice";
            }
            document.write(x);
        }
    </script>
</head>
<body>
    <form name="myscore">
        Score: <id="score"> <input type="number" name= "score">
        <input type="submit" onClick="myFunction()" value="submit">

    </form>


</body>
</html>

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0
  • Change input type from submit to button else you wont be able to see the output, because the form will submit and reload the page.
  • Fix the syntax errors <id="score"> Change } to { under else condition
  • Change document.write to alert or console.log

Comments

0

Try this:

  <script>
    function myFunction()
    {
        var x;
        var score=document.getElementById("in_score").value;

        if(score>30)
          x="Expert";
        else
          x="Novice";
        document.getElementById("score").value = x;
    }
  </script>
  <body>
    <form name="myscore">
     Score: <input id="score" type="number" name= "score">
     <input type="submit" id="in_score" onClick="myFunction()" value="submit">

    </form>


   </body>
   </html>

So the main error in your code was the reverse bracket from the else statement. Also, to get/change values from an input, you need to use getElementById ( to select the element ), and .value statement, to change/get it.

2 Comments

Your code looks good, but you should add some explanation of what is going on in it to help the OP understand what you are doing here. How do I write a good answer.
Something like this?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.