Jump to content

Cannot get my first script to execute properly

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mkjt88

mkjt88

    Newbie

  • Members
  • PipPip
  • 27 posts
Hey guys been trying to learn javascript and this is my first attempt at really making anything though its nothing :D

Anyway I don't know exactly what I should be using to get what I want. My main plan was to simply prompt for your name.. then either prompt or select your age group then something else and that's it. Now I've tried many many things like arrays etc and can't get anything to work so I tried to add two functions.. both to prompt, and dunno if this is even allowed haha.. but when I do it skips my name function and goes straight to age.. Anyone know why this is? Also if I could get some tips on what I should be using to accomplish this please let me know. thanks guys and if you don't quite understand what I'm asking let me know.

<html>

<head>

  <script type="text/javascript">

function show_prompt()

{

	var name=prompt("Please Enter Your Name","Enter Name");

  if (name!=null && name!="")

	{

	document.write("Hello "  + name +  " Please Continue");

	}			

}


 function show_prompt()

{

	var age=prompt("Please Enter Your Age","");

  if (age>1)

	{

	document.write(".. Moving along.");

	}

}

   </script>

</head>

<body>

	

<p>Please Click Med Test to Begin</p>

<input type="button" onclick="show_prompt()" value="Med Test" />


	

</body>

</html>


#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
learning javascript can be one of the most fantastic thing in the world, good you choose the right path.
First the script them the explanation
<html>
    
    <head>
        <script type="text/javascript">
            function get_name() {
                var name = prompt("Please Enter Your Name", "Enter Name");
                if (name !== null && name !== "") {
                    alert("Hello " + name + " Please Continue");
                    get_age();
                }
            }

            function get_age {
                var age = parseInt(prompt("Please Enter Your Age", ""));
                if (age > 1) {
                    alert(".. Moving along.");
                }
            }
        </script>
    </head>
    
    <body>
        <p>
            Please Click Med Test to Begin
        </p>
        <input type="button" onclick="get_name();" value="Med Test" />
    </body>
</html>

On clicking the button the function get_name executes and alert's your name that it gets from the prompt. The result of prompt wheather you enter it or not is "String" datatype
Once the name is finished we want the age , so we invoke a function by writing the function name and angular brackets after them like this : get_age()
as i told you before the result of the prompt is always a string, you need to parse the integer content of the string before comparing it with another integer. hence the ParseInt native function built into the domLastly single ' = ' will always match the type / object of the variables on right and left hand side. ' = = ' will match the variable values .there you go :D
happy scripting

#3
mkjt88

mkjt88

    Newbie

  • Members
  • PipPip
  • 27 posts
Appreciate the reply! I understand now you have to call for your next function before it executes.. can't just throw it in there. I got my code to work this time now I just need to look into more of the parseInt part to fully understand what is going on there. Once again thanks a lot and here is my code functioning.

<html>

<head>


	  <script type="text/javascript">


function prompt_name()

{

	var name=prompt("Please Enter Your Name","Enter Name");

	if (name!==null && name!=="")

		{

			alert("Hello " + name + " Please Continue");

			prompt_age();

		}

}


function prompt_age()

{

	var age= parseInt(prompt("Enter Your Age",""));

	if (age>1)

		{

			alert("Moving On");

			prompt_symptoms();

		}

}


function prompt_symptoms()

{

	var symptoms= parseInt(prompt("What are Your Symptoms",""));

	if (symptoms.length<2)

	    {

			alert("Please Enter Correct Criteria");

		}

	

	else

	    {

			alert("Please Continue for Your Diagnosis");

			prompt_diagnosis();

		}

}


function prompt_diagnosis()

{

	var diagnosis= parseInt(alert("Ok And Healthy"));


}

	  </script>

	

</head>


<body>


	<p>Please Click Med Test to Begin</p>

	<input type="button" onclick="prompt_name();" value="Med Test" />

	

</body>

</html>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users