+ 3
A question about a challenge question
var a = { atr:12 }; var b = 37; function change(obj,x){ x++; a.atr++; } change(a,b); console.log(a.atr + "" + b); I couldn't understand why a.atr is not 38. Because when I do try to increment the object a within a function, the property inside the object a increases properly. Could you tell me the reason behind that? Thanks
10 Respuestas
+ 3
//well... Its logic
var a {
atr:12
};
//global variable
var b=37; //also global
function change(obj,x){
x++; //x is a local variable and has no influence at anything
a.atr++;//increasing atr of a from 12 to 13
}
change(a,b); //making x inside the function have the value of b, wont help x influence b because they dont say b++
console.log(a.atr+""+b);
//output 1337 13 from a.atr and 37 from b since it is not changed
+ 7
~Just Another Brick In The Wall~
I just figured this out: you can put ; as many as you want
int x=5;;;;;;;;;;;
and it works 😁
(works in java, probably works in other languages)
+ 6
var a{
atr:12
};
why would you put ; after { ?
Isn't that unnecessary or maybe there's a reason?
+ 2
it's unneccessary but in this case used to confuse people with the values of a and a.atr
+ 1
I think I am confused a bit about the value of variable and property. a.atr can't be 38. It is 12. It can be 13. 😂 Thank you ~Just Another Brick In The Wall~
+ 1
Joseph Hardrock I think this question is ment to let you be confused with variables, their names, values, and how the last ones are changed
+ 1
Voja lol but I didn't read your question correct I thought you asked why they put curly brackets and atr inside it
+ 1
btw a semicolon remembers a program that it is the end of the statement line
so what you are saying to the program is
int x = 5 //New codeline
//New codeline
//New codeline
//New codeline
//New codelin
etc .....
+ 1
Maybe the confusion was generated by the differences between languages?
In Python you can define some container object like arr = [] and throw stuff in there with arr.append(x) even from a function, while you can't reassign (arr = 'whatever').
+ 1
In javascript you can also define a container object like var arr = [] and arr.push() and way more, but there is a difference here. A has (I think) the value { atr:12 } if you alert or console.log it, but if you alert or console.log an array, it will output just values separated with commas, even when you stack arrays like arr = [a [ b ] [e [ c ] [ d ] ] ] but if I alert arr[2], even though there are other values stored in it, it will output e but when I alert a.atr, it can have other values and maybe even another container like that so you can do a.atr.anotheratr.anotherthirdatr etc. and store huge amounts of values