I'm new here :)
So, in javascript, I "simply" wanted to solve X in an equation:
My original formula was:
var X = (((((k/(cc**ci))-bk*)/(0.01*kg))-18)/((0.1+0.05*d1)*(280/(w+100)))-sd)
If I recall correctly, it returned some ridiculously high number...
Ok, I guess that was one problem... so, I tried this:
var X = (((((k*1.0/(cc*1.0*ci*1.0))-bk*1.0)/(0.01*kg*1.0))-18)/((0.1+0.05*d1*1.0)*(280/(w*1.0+100)))-sd*1.0)
Now it worked quite well for these simple variables:
cc=0, ci=0, bk=100, kg=100, d1=10, w=100, sd=10
BUT.. when the solution was approaching 0, it suddenly went completely crazy.
In this case, if k=126.4 the solution should be 0. However, I'm getting "7.1054273576" instead. I calculated it with different programs (even my old pocket-calculator lol), and they all say it's 0 --> so I guess my equation should be correct.
I tried k=126.5, which returns 0.119.
and k=126.3, which returns -0.119
These are correct.
So logically k=126.4 should return 0... but it doesn't. It still returns 7.1(...) instead.
I even tried replacing all the variables with the (see above) numbers:
var X = (((((126.4*1/(1*1*1*1))-100*1)/(0.01*100*1))-18)/((0.1+0.05*10*1)*(280/(100*1+100)))-10)
--> THIS STILL RETURNS TO 7.1(...), although it should be 0.
So the problem is definitely not one of my variables.
For these tests I was using an input type="number" object to review the results, if that is of any relevance...
I still can't see the problem, this seems absolutely illogical and is a complete mystery to me. Pls help!
You missed the bit at the end. X is equal to:
7.105427357601002e-15
^
Scientific Notation!
Note the e-15 at the end. That means, in decimal notation, it's
0.000000000000007105427357601002
Or in other words, very close to 0, as far as most uses are concerned. The difference between it and 0 likely comes about by rounding.
The problem was indeed an e-15 at the end. So if anybody else is stumbling across this page they can stop wondering.
A very simple solution: Just use Math.round() in this case to reduce the number of integers behind the comma, so that "e" doesn't get used to describe the number.
Related
I've just been experimenting with a little bit of Javascript and got to the point of understanding the inheritance concept.
I am able to get an evaluation for the below code:
"".constructor
//which evaluates to function String()
Ok Cool. But why is it that when I do the below code, there is an error?
2.constructor
//returns an error
Basically both are primitives right?, so should there not be an error for the empty string as well?
Hope someone can give me a good explanation that will help me learn this one better. Looking forward to your support.
You could spend another dot for the decimal point.
console.log(2..toString());
console.log(2.2.toString());
Or wrap the value in parenthesis.
console.log((2).toString());
console.log((2.2).toString());
So I am trying to convert a string to an integer in javascript and then comparing it to an integer variable I created. I have read many many things online but nothing worked so far. This is my code:
var followedUsers = $('.ProfileNav-item.ProfileNav-item--following.is-active').find('.ProfileNav-value').html();
so when I type followedUsers in console I get "1256". I tried parseInt as follows:
var x = parseInt(followedUsers,10);
But when I type x in console I get 2. Yes it is an integer but its not correct integer.
I also tried parseFloat. It gives the same result. This one didn't work either.
var x=parseInt(followedUsers.valueOf(),10);
I dont know if this is a thing but I was just messing around trying different things. I saw another guy's question. So I gave that a shot:
var x = +followedUsers
This one gave me a NaN.
var x = followedUsers*1
This one also gave me a NaN.
I tried converting my integer value to a string and comparing them that way but that doesn't gives me correct outputs. I am running out of ideas guys, am I missing something here? Changing a string to an integer shouldn't be this hard, right?
Perhaps followedUsers isn't what you think it is.
I made a simple example in JSBin, trying to simulate you problem and everything is working:
http://jsbin.com/huzegovaxa/edit?html,js,console,output
Try getting followedUsers with .val() insted of .html()
It turns out that the old browser in my university computer lab doesn't support parseInt or any other methods I mentioned above. It worked fine when I tried it on other computers. Although I believe this post will be useful for anybody looking for conversion in means of seeing all the methods in one place
Try
var x = parseInt(followedUsers);
I am doing the video tutorials from The New Boston and I'm on video 20. Everything has been fine until now but I can't get this one to run. I've checked against the code in the tut and I just can't see what I'm missing. I'm sure it's right in front of my face.
Sorry this is so basic, but I'm a total newbie.
This is the video tutorial: http://www.youtube.com/watch?v=QPFW_0blw9w
This is my code:
var x = 1;
while(x<10){
document.write(x + "how do you like </br>");
x++;
}
Thanks for any help!
Your syntax is correct, as verified by running the JavaScript code (exactly as you wrote it) inside Codecademy Labs. If your while loop isn't working, I suspect it has to do with your environment or the state of the document being written, but it is not the code itself. As a fellow newbie, I try running code in two IDE's (codecademy labs being one) to test a lot of code and confirm against this sort of issue, though codecademy can't do file operations.
In addition, if you're interested in knowing why a for loop is superior to while for this situation, continue reading.
A for loop is a loop that comes in handy when you have a fixed index; When you know how many iterations you need, or know how to find out and can work that into the code.
A while loop is better at handling situations when an unknown (and unpredictable) number of iterations is required, as it will loop through until the conditions are eventually met.
Keep in mind that what makes it strong also makes it weak; for loops are harder to create infinite loops for. Because of the arguments in a for loop (IE: for (index,index<=value,value++)), most for loops self-terminate neatly, or at least it's hard to forget to put the "closing" condition in. while loops, however, don't innately ask for a closing statement and thus you must explicitly state them inside the loop.
I hope this answers your question, and more.
EDIT: Oh yeah, and </br> doesn't work inside the quotes. You can remove it, and it should still do a new line without it; But leaving </br> inside the code just has it literally print </br> in the line.
I don't see anything wrong with your code the way you wrote it, so there must be an error in some of the code that surrounds this. Alternatively, for the same outcome as this, I would suggest using a for loop as such:
for (var x = 1; x < 10; x++){
document.write(x + "how do you like </br>");
}
In JavaScript, I'm building something like tic-tac-toe (but more complex). Any given field can have three values: a black piece, a white piece or nothing. What would you use to represent these values? Considering you're gonna be passing around about 300+ at a time.
I was first thinking of 'B', 'W' and 'N'. Then I thought of 0, 1 and 2 and now I'm thinking about true, false and null. Which is the better option as far as JavaScript is concerned? Faster? More idiomatic?
It is not going to make a speed difference.
Personally, I'd go with 0,1,2.
In general, I'd avoid making a difference between undefined, null and false. That will just result in some mistakes in a conditional somewhere. In your case, undefined seems a pretty poor choice anyway, because the contents of the field are not undefined/unknown, it is well-defined to be empty.
As Thilo points out you'd want to avoid having multiple values that can make the statement if(position) evaluate position as true. I'd probably go with using null as the empty value, though, as it seems to make more sense to me.
For the other values, I'd definitely go with something that it's hard to mix up. Strings aren't great, because you're going to wind up using B instead of W somewhere along the line and get annoying errors, so 1 and 2 probably aren't bad options.
Another option, though, is to make your own "enum" for the purposes - just have var black = new Object() and var white = new Object() and use those. That way it's a bit more clear what you're referring to than just 1 and 2. Much of a muchness, though.
Assuming I have a numerical string:
var foo = "0";
Assume I want to parse it as a number, and increment it assigning the value to bar (just an arithmetical example), I know I could do:
var bar = parseInt(foo, 10)+1;
But I believe the above example could be simpler and more readable. Consider this example:
//alternative 1
var bar = +foo+1;
And this one:
//alternative 2
var bar = ++foo;
Both of these will also yield the same result for bar (the latter will also increment foo, but that's no problem).
My question is, by using one of these alternatives, would I be exposing my code to the same possible implementation-dependant flaws of using parseInt without specifying a radix/base?
Or are these safe to use as well?
Fiddle for testing.
From the perspective of readability and maintainability parseInt is the best choice. It clearly tells what it does and does it reliably.
Tomorrow, when a fellow developer joins the project, of after a couple of months when you yourself will be looking at your own code, or if you suddenly decide to increment by ten instead of just one, you'll be in less trouble working with parseInt.
Also, try JSLint. It's worth it.