Search code examples
javascriptfunctionargumentssemantics

Simple JS with deffining functions


I'm creating a simple questionare using prompts for a website, but i am having issues.

I am recieving two errors with this script, firstly "question is not deffined" ,

secondly "missing ) after argument list" on the line with my first prompt any ideas?

<script>
var a = Math.floor((Math.random()*10)+1);
var b = Math.floor((Math.random()*10)+1);
var c = Math.floor((Math.random()*10)+1);

var wrong = 0;

function question()
{   
    for(x=1; x>10; x++)
    {
        prompt("Does" b"+"c " = ");
        if(prompt.input == b + c)
        {
            question();
        }else{
            wrong++;
        }

        if(x==10)
        {
            alert("well you were wrong " + wrong" times");
        }
    }
}
</script>

Solution

  • You are missing all + in the argument to

    prompt("Does" b"+"c " = ");
    

    You have to use + to concatenate strings:

    prompt("Does " + b + "+" + c + " = ");
    

    The same + is missing in:

    alert("well you were wrong " + wrong" times");
    

    Use:

    alert("well you were wrong " + wrong + " times");
    

    Also, you are calling question from within itself. This doesn't cause a syntax error, but it's hardly desired in your case.


    Also, prompt.input doesn't work. It's always undefined. Use the return value of the prompt call:

    var response = prompt( ... );
    if(response == b+c){
      ...
    

    Also, you are only initialising your random variables once. Perhaps you want a new pair within each loop (unless the recursion was meant for that). Thanks @Asad for noting.