Jump to content

Need a little help


Random

Recommended Posts

I'm fairly new to javascript and I have been having trouble learning it. I need to finish a project where I have to be able to type in a number and depending what that number is a message will appear on the screen. Like if I type a number between 1 and 10 message A will appear, 11 to 20: Message B. I have the if else statement set up, I'm just having trouble displying it. If anyone knows off hand how to do it or has an example or a direct link on how to do this I'd really appreciate it. I've searched all over the web and cant seem to find it. Thanks.

Link to comment
Share on other sites

<html><head>	<title></title>	<script>			var num = prompt("Enter a number","");		showMessage(num);			function showMessage(num)		{			if(!isNaN(num))			{				if(num < 1)					alert("The number needs to be greater than 0");				else if(num > 0 && num < 11)					alert("1 to 10");				else if(num > 10 && num < 21)					alert("11 to 20");				else					alert("Greater than 20");			}			else				alert("You need to enter a number!");		}		</script></head><body></body></html>

Link to comment
Share on other sites

Now that I finished your homework for you...did you learn something? :)

<html><head>	<title></title>	<script>			function showMessage()		{			var num = parseInt(document.getElementById("num").value);			var msg = "";			if(!isNaN(num))			{				if(num < 1)					msg = "The number needs to be greater than 0";				else if(num > 0 && num < 11)					msg = "1 to 10";				else if(num > 10 && num < 21)					msg = "11 to 20";				else					msg = "Greater than 20";			}			else				msg = "You need to enter a number!";							document.getElementById("message").value = msg;		}		</script></head><body>	<input id="num"> <input type="button" value="Send" onclick="showMessage()">	<br><br>	Message:<br>	<textarea cols="30" rows="2" id="message"></textarea></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...