I have a block of code that uses Math.random(). I noticed that occasionally the return value would be "Undefined". This is the code I used:
return data.map(val => {
var r = Math.random();
if (r < this.mutChance) {
console.log(Math.random);
debugger;
return this.rDna(val);
}
return val;
});
When i set the mutChance variable to 0 and let the code run for a bit eventually debugger gets called and shows the value of r to be undefined. I tried to reproduce the problem by running in console
var test = Math.random();
while(test){
test = Math.random();
}
However, this loop never ended. I have no idea why the function would act differently within my object, and the console.log(Math.random); Says that the function still has its native code. Nowhere do I override the random function, nor do I use the variable r anywhere else.
I am relatively new to JavaScript and couldn't find this problem anywhere else. The only other code I have imported is the p5.min.js package.
Problem was with how chrome was interpreting the very small values
Without console.log chrome shows it like this
With console.log chrome displays correctly
Related
I am facing a really odd problem when executing my JS code. My code execution is not entering a for loop and there are no errors in the console. When I try executing the loop by typing directly in the console, it is executing. Here is the loop :
for(var x = 0; x <= distance; x++) {
var yMinusY1Sq = distance*distance - (x - startX)*(x - startX)
var yMinusy1Cal = parseInt(Math.sqrt(yMinusY1Sq));
console.log(yMinusY1Sq, yMinusy1Cal);
if((yMinusY1Sq == yMinusy1Cal*yMinusy1Cal)) {
y = yMinusY1Cal + startY;
var point = document.createElement("div");
point.className = "output-point";
point.style.height = (grid.offsetHeight/16).toString() + "px";
point.style.width = (grid.offsetWidth/16).toString() + "px";
outputPoints.appendChild(point);
point.style.top = y*oneBoxY;
point.style.left = (x+startX)*oneBoxX;
isodistancePoints.push(point);
}
}
Here, distance is >= 1. The console.log inside the loop is not executing and printing anything. While, if another console.log is put just before the loop, it is printing. So, what is going wrong?
Edit
I tried printing distance, which is a global variable, just before the loop, it is showing undefined, but if it is printed directly using the console, it gives a number value. Here is a screenshot:
Here, is the order of initialization and function, the other global variables initialized are working fine.
var distance;
init();
function init() {
distance = 1;
//Other code
}
$("#run").bind('click', function () {
// some other code
for(var x = 0; x <= distance; x++) {
// some code
}
});
The problem you are facing is in the console. What gets logged is a reference not the value of the object in time. From MDN
Please be warned that if you log objects in
the latest versions of Chrome and Firefox what you get logged on the
console is a reference to the object, which is not necessarily the
'value' of the object at the moment in time you call console.log(),
but it is the value of the object at the moment you open the console.
And the solution is
console.log(JSON.stringify(distance))
or
console.log(`${distance}`)
The problem was that the loop was inside an else statement, and the if of the statement had a local variable named distance. I still don't know the exact cause of the error, as the execution is not supposed to go into the if because its condition is not satisfied, but renaming the variable worked and now I am getting desired output. But if anyone knows the exact reason, please comment.
I just learned about the ternary operator but it is not functioning like I expected. If find it really confusing. I get an error in the console over and over again, and I don't understand why.
A normal function gives me undefined, which is fine, but the ternary operator gives me a "not defined" error, but why?
Ternary Operator
var experiencePoints = winBattle() ? 10 : 1;
Error
VM363:1 Uncaught ReferenceError: winBattle is not defined
My function
function experiencePoints() {
if (winBattle()) {
return 10;
} else {
return 1;
}
}
And it gives:
undefined
I want to get undefined just like the normal function gives.
The error is not because you used the ternary operator.
The message is telling you that JavaScript cannot find a function named "winBattle()" anywhere in your code - or at least, not within the current scope.
As we will see in this demonstration, if you declare such a function, and make it return a simple boolean "true" value (just for example), then the error does not occur:
var experiencePoints = winBattle() ? 10 : 1;
console.log(experiencePoints);
function winBattle()
{
//I assume here you would have some logic to calculate the winner of the battle, and then return true or false depending on who won.
return true;
}
You will need to check the rest of your code, and either
a) create the function, if it doesn't exist
or
b) make it accessible from the scope where you are calling it. If you need help with this task, you will have to show us the rest of your code.
Here is some background information:
I think you may have mistaken the undefined you're seeing as the result of executing the "experiencePoints" function. It is not. It is simply the result of creating that function via the console. The console always shows the result of the line you just created, which in this case is nothing, because you're just declaring a function, not running anything which produces output. If you included that function in a web page you would not see such a message. You have never actually run that function. If you did (by writing experiencePoints();) you would almost certainly see the same error relating to winBattle(), since at the time you run the function, winBattle() does not exist.
The difference between that and your ternary operator code is that this line of code is not within a function, and is therefore executed immediately.
The two are not the same: the function experiencePoints only executes when it is called, but you are not calling it. Instead you enter the function definition itself which does not return anything, and so you see undefined in the console.
The variable assignment with the ternary operator is executed on-the-spot (it is not a function definition), and so winBattle must exist at that very moment. Apparently it doesn't, and so you get an error. If you would just do this:
var a = Math.random() > 0.5 ? 10 : 1
You will not get an error, because Math.random is defined. Also you will see undefined in the console, which is normal for a statement, like var.
Coming back to the function experiencePoints: you may wonder why you don't get the same error about winBattle there. It is because the function is not executed yet.
Until you actually call it, you still have time to define winBattle. If however you decide to call it without first defining winBattle, you will get that same error.
Now you can of course use a ternary operator in a function -- that would be a fair comparison. You can choose between several syntaxes. Here are two:
function experiencePoints {
return winBattle() ? 10 : 1;
}
or as arrow function with expression syntax:
var experiencePoints = () => winBattle() ? 10 : 1;
Again, here you would only get an error about winBattle when you call the function.
Is this
var result = XrmServiceToolkit.Soap.Execute(setStateRequest);
just storing the function into the variable,
executing and storing the return value into the variable,
or doing both?
Unfortunately I wasn't able to find something useful in the internet. Looking at http://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions
it looks like the function is executed but i am not sure.
I also tested it with a normal Javascript inside the chrome browser and got this result:
> function test(a){
console.log(a);
};
undefined
Calling the function normal
> test("asd");
asd
With a variable declaration
> var x = test("asd");
asd
But it looks like the variable does not contain any information
> console.log(x);
undefined
> x
undefined
Now I am completely confused. Why is the function called as variable when it is never stored? I am new to Javascript and need to understand what this is exactly doing.
It is storing the return value of the function into a variable.
The reason your test function is not working is because you don't return a value in test.
function test(num) {
return num * 2;
}
var doubled = test(2);
// doubled now contains 4
var doubleVariable = test;
// doubleVariable is now the same as test
doubleVariable(2)
// returns 4
This article may clarify things a bit more
I'm reading someone's javascript code and I have a hard time understanding what they are trying to do here.
var add3 = add(3);
var add4 = add(4);
console.log(add3(4));
Can someone explain what is going on inside the console.log() here?
Is console.log just taking the add3 value and automatically adds it to a integer 4?
Thanks
window.console is how your browser gives feedback on the document it has downloaded and parsed. You can usually see it by pressing F12 and then clicking the "console" tab. It's a good substitute for alert, but you can also write JavaScript directly into it and then click "Run" (or press enter if it's a one-line command box). That's much easier than writing it to a file, saving it, refreshing, and seeing what happens.
Not knowing anything about your add function, it looks like it's meant to show an example of currying. So instead of saying:
function add(x, y){
return x + y;
}
You write:
function add(x){
return function(y){
return x + y;
}
}
Then you can do:
var add3 = add(3); //returns a function that will add 3 to anything
console.log(add3(4)); //returns 7.
console.log(add(3)(4)); //also returns 7.
This seems like a silly way to do it, but it's a way to generate functions on the fly. If I did add(3) to the first example, it would break and say "y is undefined" in the console. Using the curried example, var add3 = add(3) is like saying "well, I don't know what I want to add three to yet, so add3 will just be another function that will add 3 to anything."
console.log outputs its argument to the console.
Its arument (add3(4)) is a function call, that calls the function add3 with the argument 4.
add3 is a function that is generated by add.
The function add looks lke this (probably):
function add(n) {
return function(x) {
return n + x;
}
}
I have run in to a stupid problem...
I declared a new variable called leadingZero. I save the modified .js file and run the project with a breakpoint on the leadingZero assignment and in watcher window it says its undefined after passing this line, but all the other declarations here are working fine and I can see the assigned values. needless to say the getObject call does not work now.
var leadingZero = 0; //new variable
var chkActive;
var chkSubscribe;
var hdnItem = getObject('hdnItemCounter');
var ItemCount = parseInt(hdnItem.value) + 1;
for (intCounter = 2; intCounter <= ItemCount; intCounter++) {
chkActive = getObject('dgrProductList_ctl0' + leadingZero + intCounter + '_chkActive');
}
Check this http://jsfiddle.net/DHDsE/
Not getting the undefined problem tho, but having to add toString() to leadingZero for it to render in the console.log, so maybe that's your issue too.
You did set the breakpoint on the line below, didn't you?
Because if you set it on the line var leadingZero = 0; it halts before the line is evaluated, which explains the undefined value in the watcher.
Also, as gillesc pointed out, your leadingZero must be a string, otherwise you're adding up intCounter and leadingZero, rather than concatenating them.
The problem seemed to be changes to the js were not loaded in ie cache. even after closing ie, rebuilding the project and running again, I still need to hit ctrl+f5 on the page to load the new javascript