Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Executing Functions

    Hey guys, I'm new to JS and I have been having a lot of trouble with functions lately

    here is the JavaScript I have so far (there are buttons, html ect. that I did not include)

    Code:
    main();
            
        	    function main() {
                    document.getElementById("StartProcessButton").onclick = startProcess;    
    	    }
    	    
    		    function startProcess() {
    		     
    		    var value = prompt("Enter the value");
    		    
    		    if  
    		    (value == realNumber)
    		    alert("Correct");
    		    else
    		    confirm ("Wrong")
    		    }
    		    
    		function myRealNumber() {
    		    var numValue1 = document.getElementById("Lowest");
    		    var myText = numValue1.value;
    		    var LowestButton = Number(myText);
         
    		    var numValue2 = document.getElementById("Highest");
    		    var myText = numValue2.value;
    		    var Highest = Number(myText);
         
    		    var realNumber = (Math.ceil(Math.random()*"Highest"+"Lowest"));
    		}
    How do I make my function "myRealNumber" execute when function "startProcess" does? specifically how do I make var "realNumber" execute when "startProcess" does

    please let me know as to the errors in the code
    any help is greatly appreciated! thanks so much!

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Why do:

    Code:
    main();
            
    function main() {
        document.getElementById("StartProcessButton").onclick = startProcess;    
    }
    When you can easily do:

    Code:
    <input type="button" id="StartProcessButton" value="Test" onclick="startProcess()" />
    And certainly, to call a function from within another function....

    Code:
    function startProcess() {
        myRealNumber();
        var value = prompt("Enter the value"); 
        if (value == realNumber)
            alert("Correct");
        else
            confirm ("Wrong")
    }
    And you'd want to define a global variable inside the second function, rather than declaring a local one (Which won't be accessible by startProcess()) and certainly using a string to do arithmetics... like in your case with *"Highest"+"Lowest" won't do a whole lot of good:

    Code:
        realNumber = (Math.ceil(Math.random()*(numValue1+numValue2)));
    That should about do the trick. Haven't tested your math part, but it seems clearly impossible to get a correct answer when you're randomly generating an integer to include in the calculation.

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've always heard to stay away from global variables, but thanks for the response!
    I was wondering if maybe I could just return the value of realNumber and then define it as a variable in the startProcess function

    or wouldn't that work :/

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    You certainly could, but I see no problem with defining a single global variable. It's not like you'll be generating realNumber ten times in the same page and calling it might get confusing. Plus, if you end up re-declaring it somewhere, it won't really affect anything else, I don't think. Especially when the function that declares it originally is only triggered on a click. Also, I apparently hadn't quite understood your math logic, so... the variable definition would look like:

    Code:
        realNumber = (Math.ceil(Math.random()*(Highest+LowestButton)));
    Instead of the one I gave you earlier. Once again, global variables aren't all that evil and scary, and returning a value from a function that isn't something incredibly long and complicated shouldn't really be needed.

  • Users who have thanked Eldarrion for this post:

    Jamie315x (03-13-2009)

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I am more interested in learning the process than the actual example but I see what you mean.
    If I were to return the value and then define it as a variable in function startProcess how would I go about defining the variable?

    thanks again, I appreciate the help

  • #6
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    It would work something like this:

    Code:
    function startProcess() {
        var realNumber = myRealNumber();
        var value = prompt("Enter the value"); 
        if (value == realNumber)
            alert("Correct");
        else
            confirm ("Wrong")
    }
    
    function myRealNumber() {
        var numValue1 = document.getElementById("Lowest");
        var myText = numValue1.value;
        var LowestButton = Number(myText);
       
        var numValue2 = document.getElementById("Highest");
        var myText = numValue2.value;
        var Highest = Number(myText);
        var realNumber = (Math.ceil(Math.random()*(Highest+LowestButton)));
        return realNumber;
    }
    Doesn't take too much code to achieve the desired effect. (marked in red) Of course, if you need to call the variable anywhere else, it won't be accessible.

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Eldarrion View Post
    It would work something like this:

    Code:
    function startProcess() {
        var realNumber = myRealNumber();
        var value = prompt("Enter the value"); 
        if (value == realNumber)
            alert("Correct");
        else
            confirm ("Wrong")
    }
    
    function myRealNumber() {
        var numValue1 = document.getElementById("Lowest");
        var myText = numValue1.value;
        var LowestButton = Number(myText);
       
        var numValue2 = document.getElementById("Highest");
        var myText = numValue2.value;
        var Highest = Number(myText);
        var realNumber = (Math.ceil(Math.random()*(Highest+LowestButton)));
        return realNumber;
    }
    Doesn't take too much code to achieve the desired effect. (marked in red) Of course, if you need to call the variable anywhere else, it won't be accessible.
    Ah, well thanks so much for the help
    I do have one more question if you have the time to answer it
    I know that my random function works with numbers shouldn't my Id's work as well? they correspond with the textfield id so Im not sure why they arn't working.

    I really appreciate your help thanks alot!

  • #8
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    They could be made to work, although considering you're already assigning the value of the item to a variable... I certainly don't see why you'd want to take that approach. Also... the mathematical functions assume that what you're giving them is what you want to calculate so "Lowest" + "Highest" would try to directly add the text "Lowest" to the text "Highest", as opposed to going so far as to looking up the value of the element with the same id. All in all, when passed a string, the plus sign works as a concatenation of two strings, resulting in the string of "LowestHighest" in the end. If you want to reference IDs, you'd be doing something like:

    Code:
    document.getElementById("Lowest").value + document.getElementById("Highest")
    As you can see, there really isn't much point in doing that, and it's much easier to use the variables you assign earlier:

    Code:
        var numValue1 = document.getElementById("Lowest");
        var myText = numValue1.value;
        var LowestButton = Number(myText);
       
        var numValue2 = document.getElementById("Highest");
        var myText = numValue2.value;
        var Highest = Number(myText);
        
        var realNumber = (Math.ceil(Math.random()*(Highest+LowestButton)));
    Now, in this case, you're clearly not passing a string (due to lack of quotes), but a variable and JS goes deeper and looks up the value of that variable (Which can also be a string, but in this case is not) A good reading on the subject of operators versus variable types could probably be found here:

    http://www.w3schools.com/js/js_operators.asp
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •