wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Undefined in alert dialog box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Undefined in alert dialog box" Watch "Undefined in alert dialog box" New topic
Author

Undefined in alert dialog box

Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32
Hello!

I am currently in a beginning web development class and am in the process of writing a very simple JS script. The objective is to print the numbers 5 through 15 with their squares and cubes to an alert dialog box. I keep trying different methods of doing this, but it always ends up with the alert dialog box saying "Undefined". I can get it to print correctly with document.write, just not with alert.

I know that I am probably overlooking something small and silly, but I just can't figure it out.

Any help would be greatly appreciated, thank you!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

I see no alert in your code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32
Sorry about that, forgot to put that in

I have been putting the alert statement at the bottom like so:



Of course the problem here is that I simply am telling the alert dialog to run a function that writes to the document, not the dialog box. That is in essence my problem; how would I print the output of the for loops into an alert box?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Well, the numbers() function doesn't return a value, so of course the value alerted will be undefined.

What are you expecting it to do?
Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32
That's a good point, I tried using return once but couldn't figure out how to make it return the table of numbers being put out by the for loops.
Could I put the loops in the return statement?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

No.

document.write() is a dinosaur that used to be used to emit HTML to the browser. If that's not what you are wanting to do, then you shouldn't be using it.

Maybe you can start by backing up and telling us what you are really trying to accomplish.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Kody Wright wrote:Could I put the loops in the return statement?


No, that doesn't make any sense. Just build a string containing the data you want to return. Build it bit by bit. Then when your loops are all finished, return that string.
Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32
Bear Bibeault wrote:No.

document.write() is a dinosaur that used to be used to emit HTML to the browser. If that's not what you are wanting to do, then you shouldn't be using it.

Maybe you can start by backing up and telling us what you are really trying to accomplish.


By "what I'm really trying to accomplish" do you mean what I said at the beginning in my first post? I'm in a beginning web development class and I'm simply trying to output the numbers 5 through 15 with their squares and cubes in an "alert" box. I've never really spent much time programming before so I am new to this and am probably overlooking a simple mistake.

Document.write may very well be a dinosaur, but it's what my book tells me to use on half the exercises...so I do.
Kody Wright
Ranch Hand

Joined: Mar 06, 2011
Posts: 32
Thanks for the advice, Paul. I used a string as you suggested and got it working just fine.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Undefined in alert dialog box
 
Similar Threads
Return string from javascript function and display in text area.
Date comparision
value undefined....how to remove that?
Calling ActiveX method in NetScape
Dialog Arguments