L8V2L Posted August 1, 2014 Share Posted August 1, 2014 (edited) 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 August 1, 2014 by L8V2L Link to comment Share on other sites More sharing options...
davej Posted August 1, 2014 Share Posted August 1, 2014 <!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> 1 Link to comment Share on other sites More sharing options...
L8V2L Posted August 1, 2014 Author Share Posted August 1, 2014 (edited) 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 August 1, 2014 by L8V2L Link to comment Share on other sites More sharing options...
davej Posted August 1, 2014 Share Posted August 1, 2014 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 More sharing options...
JamesB Posted August 1, 2014 Share Posted August 1, 2014 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 More sharing options...
L8V2L Posted August 2, 2014 Author Share Posted August 2, 2014 (edited) 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)-3The 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-3undefinedSo 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 August 2, 2014 by L8V2L Link to comment Share on other sites More sharing options...
L8V2L Posted August 2, 2014 Author Share Posted August 2, 2014 (edited) "Use strict";console.log("n"+ToInteger(1000000000000000000000.5)+"n"+parseInt(1000000000000000000000.5));//output1e+211undefinedSomeone give me a like for figuring it out(more so coming across why in the book) myself!... But please give an explanation. Edited August 2, 2014 by L8V2L Link to comment Share on other sites More sharing options...
L8V2L Posted August 2, 2014 Author Share Posted August 2, 2014 "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 More sharing options...
justsomeguy Posted August 4, 2014 Share Posted August 4, 2014 So I'm reading speaking JavaScript and I don't know why the author defining a function that already existFirst, 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 More sharing options...
L8V2L Posted August 5, 2014 Author Share Posted August 5, 2014 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 More sharing options...
justsomeguy Posted August 5, 2014 Share Posted August 5, 2014 The complete surrounding one. Link to comment Share on other sites More sharing options...
L8V2L Posted August 5, 2014 Author Share Posted August 5, 2014 The complete surrounding one.The window property? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2014 Share Posted August 5, 2014 If the loop is not in another function, then yes. Link to comment Share on other sites More sharing options...
L8V2L Posted August 6, 2014 Author Share Posted August 6, 2014 (edited) 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 August 6, 2014 by L8V2L Link to comment Share on other sites More sharing options...
thescientist Posted August 6, 2014 Share Posted August 6, 2014 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 More sharing options...
L8V2L Posted August 6, 2014 Author Share Posted August 6, 2014 (edited) Can someone explain to me the computer term bootstrap, I can't grasp the concept.And backend, and backbonejs. Edited August 6, 2014 by L8V2L Link to comment Share on other sites More sharing options...
JamesB Posted August 6, 2014 Share Posted August 6, 2014 Back End = Server-side = Code running on the server. eg. PHP, ASP, SQL Front End = Client-side = Code running on the client. eg. JavaScript, HTML, CSS http://en.wikipedia.org/wiki/Backbone.js http://en.wikipedia.org/wiki/Bootstrap_(front-end_framework) https://www.google.co.uk/?gws_rd=ssl#q=define+bootstrap Link to comment Share on other sites More sharing options...
johncc Posted August 7, 2014 Share Posted August 7, 2014 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 More sharing options...
L8V2L Posted August 7, 2014 Author Share Posted August 7, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now