a javascript function does not work on the console - javascript

I am following a tutorial to learn javascript and I am trying to run my function in the console to see the output like this
and this is my code
var output = [];
var count = 1;
function fizzBuzz() {
output.Push(count);
count++;
console.log(output);
}
but instead, it shows like this in the console
so what is the reason for this? Could you help me please
also I am a very beginner

To call a function you have to end with (), otherwise it just shows you what the actual function is as it would a variable, so fizzbuzz gives you the content of the function, fizzbuzz() executes the function.
As a side note, the p in push should be lowercase.

Related

JavaScript object as parameter

I tried to make a function that writes some divs in an HTML document. Everything is working fine but I want to try to change the function declaration from this:
function writeDiv(divDOMelement, numberOfExcercises, numberOfTasksInEx);
To something like this
function writeDiv(divDOMelement, {numberOfExcercises:N, numberOfTasksInEx:[v]})
Just to be clear, the first declaration works perfectly, but when I tried implementing the second one, and changing the function call I received an error.
Here is the full code:
let open = false;
let currentlyOpen;
function writeDiv(divDOMelement, {numberOfExcercises:N, numberOfTasksInEx:[v]}) {
for (let i = 0; i < numberOfExcercises.N; i++) {
// ... didnt include the code that prints the divs on the site just to make it easier to read.
for (let j = 0; j < numberOfTasksInEx.v[i]; j++) {
// ... same as the first loop.
}
}
}
$(document).ready(function () {
writeDiv("#mainDiv", {numberOfExcercises:3, numberOfTasksInEx:[5,2,3]});
});
Just to clarify, this numberOfTasksInEx should be an array, the first element of the array tells me that the first exercise will have that many tasks, the second tells me that the second exercise will have that many tasks, and so on.
The error I am getting:
jQuery.Deferred exception: numberOfExcercises is not defined
Uncaught ReferenceError: numberOfExcercises is not defined
Your declaration syntax is incorrect. All you need is:
function writeDiv(divDOMelement, {numberOfExcercises, numberOfTasksInEx})
Then inside your function you'll have numberOfExercises as the overall count, and numberOfTasksInEx as the array of per-exercise tasks.
The declaration syntax extracts those properties from the single object passed in as the second parameter to the function in a process called "decomposition". The : in your version does something, but not what your code expects: it allows the property name from the source object (the actual parameter) to be renamed. That's not really necessary here, but for example numberOfExercises:N would mean that in your function you would refer to simply N, not numberOfExercises.N.

Is JavaScript function.bind() supported in google sheets?

Here a simple code i'm trying to run in google sheets script.
The purpose is supplying the foreach callback function additional parameters.
function print(str, num) {
Logger.log(str + ": " + num);
}
function test()
{
var array = [1,2,3];
var str = "Stam";
//This line has an exception
// TypeError: Cannot convert null to an object
array.forEach(print.bind(null, str));
}
test();
this code is based on the solution described here.
I know there are other solutions, though i want to understand why this one does not work.
I wounder maybe it is not supported with google sheets.
How about this answer? Please think of this as just one of several possible answers.
At Javascript, when null of bind(null, str) is used, this is used. At Google Apps Script, when null is used for bind(null, str), an error occurs like "Cannot convert null to an object". I think that this might be the specification of Google side. I'm not sure whether this is modified in the future update. So as the current workaround, how about using this instead of null? Or if you want to use bind(null, str) like null under the strict mode, how about using {} instead of null like bind({}, str)?
By the way, I think that test(); at the last line of your script can be removed. Because in your case, when test() is run at the script editor, test() is run 2 times by test(); at the last line. test(); at the last line is run as the global.
From above situation, when your script is modified, how about the following modification?
Modified script:
function print(str, num) {
Logger.log(str + ": " + num);
}
function test() {
var array = [1,2,3];
var str = "Stam";
array.forEach(print.bind(this, str));
}
or you can also modify test() of your script as follows. The following test() retrieves the same result with above one.
function test() {
var array = [1,2,3];
var str = "Stam";
array.forEach(function(e) {print(str, e)});
}
Result:
When you run test() at the script editor, you can see the following result at the log.
Stam: 1
Stam: 2
Stam: 3
Reference:
Function.prototype.bind()
If I misunderstood your question and this was not the direction you want, I apologize.

JavaScript Math.random() returns "Undefined"

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

How forEach works if we pass more than one argument?

I saw a javascript code where we have to print an array in the console.
I want to know how the value of i is initialised to 0 and how it is incremented.
Here is the code:
var tos = ["Hello","Hi"];//To print this arraytos.forEach(fuction(toso,i){console.log(i + " " + toso);});
I think youre messing up forEach and a regular for loop. The forEach function is a regular (built in) function, that behaves quite similar as this:
function forEach(func){ // here you pass a function as parameter
for(var i=0;i<this.length;i++){
func(this[i],i);//now it is called
}
}
So in your case, the built in function will do:
func("Hello",0);
func("Hi",1);
And thats what your parameters toso and i catch...
In your case you could also do:
tos.forEach(console.log);

Javascript, SOAP, Execute, Variable and function call

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

Categories