Jump to content

It doesn't make sense! ...or does it?


L8V2L

Recommended Posts

If you want to check whether a value is NaN, you have to use the global function isNaN(): > isNaN(NaN) true > isNaN(33) false However, isNaN does not work properly with nonnumbers, because it first converts those to numbers. That conversion can produce NaN and then the function incorrectly returns true: > isNaN('xyz') trueI don't understand that last part! It doesn't make sense.Isn't this:>isNaN("abc");TrueWhat I want to be true? The string "abc" is not a number. So true is what I would want as my result. I could understand if he was referring to:

"use strict";var foo, bar, baz;foo = "123";console.log(typeof (foo));//=> stringconsole.log(isNaN(foo));//=> falseconsole.log(typeof (foo));//=> string/*this make more since to me cause of the pitfall that can a occurred:*/bar = 5;baz = bar + foo;console.log(baz);//=> 5123
Edited by L8V2L
Link to comment
Share on other sites

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style>td,th{border:1px solid #999;}</style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>window.onload = function(){    var x = [];    x[0] = '3 a';    x[1] = '0xa';    x[2] = '123 123';    x[3] = 123;    x[4] = '1e7';    x[5] = true;    x[6] = '  ';    x[7] = '';    x[8] = 'abcdef';    x[9] = '  123  ';    var str =  '<table>';    str += '<tr><th>#</th><th>value</th><th>typeof</th><th>isNaN</th><th>Number</th><th>parseInt</th><th>parseFloat</th></tr>';    for (var i=0 ; i<x.length ; i++){    str += '<tr><td>'+ i +'</td><td>'+ x[i] +'</td><td>'+ typeof x[i] +'</td><td>'+ isNaN(x[i]) +'</td><td>'+ Number(x[i]) +'</td><td>'+ parseInt(x[i]) +'</td><td>'+ parseFloat(x[i]) +'</td></tr>';     }        str += '</table>';    document.getElementById("demo").innerHTML = str;}</script></head><body><div id="demo"></div></body></html>
  • Like 1
Link to comment
Share on other sites

I give you a like, cause of the time you spent.But this is how I did it:

    var x = [];    x[0] = '3 a';    x[1] = '0xa';    x[2] = '123 123';    x[3] = 123;    x[4] = '1e7';    x[5] = true;    x[6] = '  ';    x[7] = '';    x[8] = 'abcdef';    x[9] = '  123  ';    var str =  '<table>';    str += '<tr><th>#</th><th>value</th><th>typeof</th><th>isNaN</th><th>Number</th><th>parseInt</th><th>parseFloat</th></tr>';    for (var i=0 ; i<x.length ; i++){    str += '<tr><td>'+ i +'</td><td>'+ x[i] +'</td><td>'+ typeof x[i] +'</td><td>'+ isNaN(x[i]) +'</td><td>'+ Number(x[i]) +'</td><td>'+ parseInt(x[i]) +'</td><td>'+ parseFloat(x[i]) +'</td></tr>';     }        str += '</table>';    document.write(str);
I had to escape some of the stings.I look like to me to act the way I would aspect.... If you could point out what I should be caution of...I see a couple of maybe pitfall, I'm sorry, but it'll help if you don't mind pointing out one thing. I can see how Boolean can cause trouble.... But it's to much....Okay, I think I see all the pitfall... But if you could write a quick summary.... If not, thanks for the table.Brows by like, please add the feature, so one could go back to a post he or she like easier.Thanks for replying. Edited by L8V2L
Link to comment
Share on other sites

Document.write is generally best avoided. If you need to convert a string to a number then you just have to be aware of the special cases where something surprising might occur. With Number() the surprise might be a string that is entirely whitespace or begins with 0x. With the parse functions you might not want to accept a string containing embedded whitespace.

Link to comment
Share on other sites

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

 

15.1.2.4 isNaN (number)Returns true if the argument coerces to NaN, and otherwise returns false.1. If ToNumber(number) is NaN, return true.2. Otherwise, return false.

 

9.3.1 ToNumber Applied to the String TypeToNumber applied to Strings applies the following grammar to the input String. If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

Link to comment
Share on other sites

Integers via the Custom Function ToInteger() Another good option for converting any value to an integer is the internal ECMAScript operation ToInteger(), which removes the fraction of a floating-point number. If it was accessible in JavaScript, it would work like this:

> ToInteger(3.2) 3 > ToInteger(3.5) 3 > ToInteger(3.8) 3> ToInteger(-3.2)-3 > ToInteger(-3.5)-3 > ToInteger(-3.8)-3
The ECMAScript specification defines the result of ToInteger(number) as:
sign(number) × floor(abs(number)) 
For what it does, this formula is relatively complicated because floor seeks the closest larger integer; if you want to remove the fraction of a negative integer, you have to seek the closest smaller integer. The following code implements the operation in JavaScript. We avoid the sign operation by using ceil if the number is negative:
function ToInteger(x) {x = Number(x); return x < 0 ? Math.ceil(x) : Math.floor(x); }console.log("n"+ToInteger(3.2)+"n"+ToInteger(3.5)+"n"+ToInteger(3.8)+"n"+ToInteger(-3.2)+"n"+ToInteger(-3.5)+"n"+ToInteger(-3.8));333-3-3-3undefined
> console.log("n"+parseInt(3.2)+"n"+parseInt(3.5)+"n"+parseInt(3.8)+"n"+parseInt(-3.2)+"n"+parseInt(-3.5)+"n"+parseInt(-3.8));333-3-3-3undefined
So I'm reading speaking JavaScript and I don't know why the author defining a function that already exist... Can someone clarify. I'm pretty sure the book isn't that old when it mention the latest hot topic..... You know, I reread over the beginning part of this, and... Well I took sometime to compose this, so would still like to post it so it want go to waste.... So if you don't mind to still give your answer but reframe from saying the obvious please! Thank you! Edited by L8V2L
Link to comment
Share on other sites

"Use strict";console.log("n"+ToInteger(1000000000000000000000.5)+"n"+parseInt(1000000000000000000000.5));//output1e+211undefined
Someone give me a like for figuring it out(more so coming across why in the book) myself!... But please give an explanation. Edited by L8V2L
Link to comment
Share on other sites

"use strict";var num;num = 1;function ToString(x){//notice the operation +=return(x+="");}ToString(num); // "1"num; // 1/*Is the function just taking the value, and not the variable?*/
Link to comment
Share on other sites

So I'm reading speaking JavaScript and I don't know why the author defining a function that already exist

First, it was only supported by Firefox, and even then only for a time. Second, he's using it as an example. Implementing a built-in function is a fine example.

... But please give an explanation.

Here's a hint:

The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN.

Is the function just taking the value, and not the variable?

It's not changing the value of the variable num. It is returning a different value.
Link to comment
Share on other sites

The following rules apply to for-in loops:You can use var to declare variables, but the scope of those variables is always thecomplete surrounding function.What function?

Link to comment
Share on other sites

Variables are passed on in two ways. There are two dimensions to them, if you will: Dynamic dimension: invoking functionsLexical (static) dimension: staying connected to your surrounding scopesDynamic dimension: stack of execution contextsLexical dimension: chain of environmentsClaify, please.

Edited by L8V2L
Link to comment
Share on other sites

What function?

 

you answered it yourself

 

You can use var to declare variables, but the scope of those variables is always thecomplete surrounding function.

Link to comment
Share on other sites

Can someone explain to me the computer term bootstrap, I can't grasp the concept.And backend, and backbonejs.

Edited by L8V2L
Link to comment
Share on other sites

Booting - bootstrap loader - refers to the colloquial expression of "dragging something up by it's bootstraps" = using something small to achieve a bigger result. A small segment of code is stored in ROM, performs basic initialization and loads a larger piece of code from external storage to complete the startup process.

Link to comment
Share on other sites

Environments sometimes live on after you leave their scope. Therefore, they are stored on a heap, not on a stackCan some one Claify the above.And here's a question:So heap is what hold the variable when you use it's value as an initial value for a function?-----------------------------------------------------------------Thanks you two above... Hearing how you appose to drive a car is different from driving. I'mma need to actual lay experience bootstrap to get it fully.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...