• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Undefined in alert dialog box

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see no alert in your code.
 
Kody Wright
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice, Paul. I used a string as you suggested and got it working just fine.

 
reply
    Bookmark Topic Watch Topic
  • New Topic