Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Attempt

Status
Not open for further replies.
Mar 23, 2011
3
0
0
US
I was looking for some clarification. While i get my prompt to come up as i'd like, I cant seem to get two desired effects correct.

1. Id like the only allowable entry into the prompt to be a number else id like it to display the error prompt (this actually comes up regardless of entry now).

2. Id also like the desired result to be any allowed number that is entered not just the 5 I have listed.

Any help is appreciated and thank you in advance.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"<html xmlns=" lang="en" xml:lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
/*
The code below allow user to enter a number
then the number will display in reverse order
*/
function show_prompt()
{
var number=prompt("Enter number to reverse");
if (number!=null && name!="")
{
document.write("Here is your number in reverse order " + number);
}
else
{
alert("is not a number");
}
var rNumbers = ["1", "2", "3", "4", "5"];
document.write(rNumbers.reverse());
}
</script>
</head>
<body>

<input type="button" onclick="show_prompt()" value="Enter A Number" />

</body>
</html>
 
Your if statement check fro 2 things.
1. whether number is not null,[red]number!=null[/red] and

2. whether name is not equal to an empty string. [red]name!=""[/red] However, the name variable is never set, so that is never true. Which means it goes directly to your else statement.

if you want to check for numbers use the built-in isNaN() function short for (is Not a Number). Anything but a number will return true.

So:

Code:
function show_prompt()
{
var number=prompt("Enter number to reverse");
if (number!=null && !isNaN(number))
  {
  document.write("Here is your number in reverse order " + number);
  }
else
  {
  alert("is not a number");
  }


The second part doesn't make much sense,as you can't reverse order a single number. If you want to receive several different numbers then that's an entirely different deal.


This looks a lot like homework assignment. If it is we cant hep you further as its against the site policies.

Apologies if its not a homework or school work assignment.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks for the help Phil! Actually, I develop sites strictly with html and was looking to incorporate javascript into new sites. I am self taught and was just trying to go through practice scripts from a library book.

I guess I was over thinking the second part. It doesn't have to be a sequential set of numbers. It could be 456898 for example.
 
Ahh, o.k. I see. So you want to order the number so it would end up being 988654?

Or am I still not understanding this?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
You have it correct Sir. Does using a variable "within" another make sense here?
 
Assuming all the numbers are single digits in the received input:
You'll have to separate the input to be able to order the numbers.

An array would be my choice:

Code:
var x;
var nums=new Array();
var i=0;
for(x in number){
nums[i]=number[x];
i++;
}
nums.sort();
nums.reverse();
alert(nums);
[code]

That would order the numbers, and then reverse them. 

If you are expecting numbers with more than one digit, that is numbers over nine (9), then you'd need to incorporate some type of separator like a comma and use the split() function to turn the input into an array.

[quote]
Does using a variable "within" another make sense here? 
[/quote] 
I don't think that makes sense anywhere. not sure what a variable within variable would really be. 


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown. 

Behind the Web, Tips and Tricks for Web Development. 
[URL unfurl="true"]http://behindtheweb.blogspot.com/[/URL]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top