Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
markifornia

Chapter 5: Pursue - Random.Js What Happens When You Remove The Empty String From The Numbers Variable

Recommended Posts

So I removed the empty string from numbers like so:

 

var numbers = ' ';

 

to

 

var numbers;

 

The result is that I get an undefined value before the 6 random number generated from the script.

 

 

I simply don't understand why it becomes undefined. I actually wondered why the numbers variable was set to empty even before I saw the pursue section.

 

Why isn't this okay?

 

var numbers;

 

The variable is initialized. It is ready to hold any values.

 

 

 

Thanks,

Mark

Share this post


Link to post
Share on other sites

Whenever a variable is declared in JS without assigning it a value, the value undefined (which is a unique variable type in JS and not the same thing as the string "undefined"), is assigned to that variable by default. That's why "undefined" is display before whatever value is later added to the numbers variable.

 

As a general rule, if you're going to use the += (or some similar operator) for a string variable, make sure you initialize the variable to something at least one time before that with just the = operator. If you want an empty string, just go with = ''.

  • Upvote 1

Share this post


Link to post
Share on other sites

ok thanks hartley, it is a very important general rule of thumb not mentioned much.

 

But consider the two loops below, one is using the +=, the other is reassigning the value to numbers.

 

1.

 

var numbers = ' ';

 

 

for (var i = 0; i < 6; i++) {

 

numbers += parseInt((Math.random() * 100), 10) + ' ';

 

} // complete loop

 

VS

 

2.

 

var numbers;

 

 

for (var i = 0; i < 6; i++) {

 

numbers = numbers + parseInt((Math.random() * 100), 10) + ' ';

 

} // complete loop

 

 

Would var numbers; now be ok with the second example?

Share this post


Link to post
Share on other sites

No, because numbers += ... and numbers = numbers + ... are the same thing.

The only way to avoid this problem is to either initialize the numbers variable (as in example 1) or to declare the numbers variable, but then set an initial value after that, but BEFORE the for loop, which is kind of a waste.

 

On a side note, you don't need to use the parseInt function on Math.random, since Math.random always returns a number. Furthermore, it's senseless because everything is being converted to a string when you add ' ' to it and/or assign it to the numbers variable, which is also a string.

Basically, I don't know what you're trying to do in these examples, but I don't think it's what you had in mind (if I may be so blunt).

  • Upvote 1

Share this post


Link to post
Share on other sites

That example was actually taken from the book. It may have just been Larry showing how to use the parseInt() function with the Math.random() function at the same time, who knows.

 

Correct though in that even when the numbers object is turned into in fact a number type, it reverts back into a string anyway. Might as well just use Math.random().

 

It does seem like the general rule of thumb should be to do the following:

 

var numbers = ' ';

 

Whenever that variable (or any variable) will be passed through a loop.

Share this post


Link to post
Share on other sites

Hartley, the reason why I bring this up is because in comparison to an example in the book where volume is calculated.

 

function calculate() {

 

'use strict';

 

// volume is declared, but not initialized

var volume;

 

// Get a reference to the form's radius value

var radius = document.getElementById('radius');

 

 

if (radius && (radius.value > 0) ) {

 

// calculate the volume.

volume = (4/3) * Math.PI * Math.pow(radius.value,3);

// format the volume to four decimals.

volume = volume.toFixed(4);

 

 

} else if (isNaN(volume)) {

volume = 'Please enter a numeric value!';

 

} else {

 

volume = 'Please enter a valid radius!';

 

} // End of if

 

See here the variable volume is just declared, not initialized.

Share this post


Link to post
Share on other sites

And note that in all cases of values being assigned to volume, the = operator, not the += operator, is used. That's the key.

I'll take a look at that parseInt/Math.random example in the book later. I have no clue what's up with that, but curious about Larry's explanation. Could you please give me a page number? Thanks.

  • Upvote 1

Share this post


Link to post
Share on other sites

As far as I understand this case is something like :

 

parseInt function is used to show how to convert randomly generated float numbers to integers, as random.js shows use of for loop.

volume calculator uses if(isNan(volume)) condition to check whether the incoming value is valid number, to show if-else if conditionals.

 

If a value is coming from text input, it may be treated as a number or a string. There is no guarantee that if you enter a number in a text type input it will treated as a number all the time. It is better to use parseInt or parseFloat function while making numeric calculations. I had this problem in one of my form, that numbers were not compared correctly before I used parse functions.

May be this is different if we use number type text input for HTML5 form elements.

  • Upvote 1

Share this post


Link to post
Share on other sites

Hi Sonal,

 

Thanks for the input, it is important to be aware of date type conversions. However as Hartley points out, even with parseInt() function passed, it is then concatenated with an empty string (the space), it is therefore a string anyway. But you bring up a point though, in order to pass the variable (may or may not be a string), for added precaution it is best to pass it through the parseInt() function first even before it can be passed through the Math.random() function.

 

var numbers = ' ';

 

 

for (var i = 0; i < 6; i++) {

 

 

numbers += parseInt((Math.random() * 100), 10) + ' ';

 

} // complete loop

Share this post


Link to post
Share on other sites

sonal, you made a good point, and made me realize the error in my logic. Thank you.

I didn't consider the fact that the code is designed to take the float value created by Math.random() * 100 and turn that into an integer.

Thanks a lot for stating that, as I completely missed it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...