homepage Welcome to WebmasterWorld Guest from 54.161.247.22
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Help for a newbie JavaScript student
Hannahb




msg:3700627
 9:06 am on Jul 17, 2008 (gmt 0)

I've just started learning JavaScript and I'm already stumped. I have this code:

var mytest1 = new String();
mytest1.setNumber( 2 );

var mytest2 = new String();
mytest2.setNumber( 3 );

if( mytest1 + mytest2 == 5 ) alert('Success!');

But nothing is happening when I run it! What am I missing or doing wrong? Help would be much appreciated. I'm sure it's something stupid.

Thanks all,
Hannah

 

Arno_Adams




msg:3700636
 9:48 am on Jul 17, 2008 (gmt 0)

Hi,

setNumber is no standard method for strings in javascript.

HTH, Tom

Hannahb




msg:3700644
 10:09 am on Jul 17, 2008 (gmt 0)

Thanks for your reply. What should I use instead?

adb64




msg:3700645
 10:09 am on Jul 17, 2008 (gmt 0)

Hannahb, welcome to WebmasterWorld!

mytest1 and mytest2 are declared as strings, adding like you did will not result in a value of 5 but in a string with value "23".

if( mytest1 + mytest2 == "23" ) alert('Success!');

That will probably work (I didn't check however)

Hannahb




msg:3700647
 10:09 am on Jul 17, 2008 (gmt 0)

Or is the use of "String" the problem?

Hannahb




msg:3700648
 10:11 am on Jul 17, 2008 (gmt 0)

Hi adb64. Thanks for your answer. I tried changing to "23", but that didn't work either. Just a blank screen. :(

adb64




msg:3700657
 10:29 am on Jul 17, 2008 (gmt 0)

Hi Hannahb,
I've tried and check javascript string object and like Arno_Adams (AKA Tom) mentions, setNumber is not a method for the string object.
What exactly do you want to achieve?

Hannahb




msg:3700658
 10:32 am on Jul 17, 2008 (gmt 0)

It's for an assignment for my class. Maybe it would be best to post the original question.

Write the missing implementation of Test to make this code work and the
alert appear:

var mytest1 = new Test();
mytest1.setNumber( 2 )

var mytest2 = new Test();
mytest2.setNumber( 3 );

if( mytest1 + mytest2 == 5 ) alert('Success!')

Little_G




msg:3700679
 11:22 am on Jul 17, 2008 (gmt 0)

Hi,

You need to override the Object class's valueOf method.
Like this:
function Test(){
this.number;
this.setNumber = function(number){
this.number = number;
}
this.valueOf = function(){
return this.number;
}
}

The valueOf method tells the JavaScript interpreter how to convert the object into a primitive, in this case a Test object into an int.

Core JavaScript 1.5 Reference:Global Objects:Object:valueOf - MDC [developer.mozilla.org]

Andrew

[edited by: Little_G at 11:26 am (utc) on July 17, 2008]

Hannahb




msg:3700681
 11:25 am on Jul 17, 2008 (gmt 0)

It worked! Thank you so much! :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved