newbee to Javascript...
I've got a problem with updating a global variable witht he result of a function. When I output the variable it says 'undefined'.
What I am trying to do is loop though an array (problemArr) and create a string in to a variable (stringA).
Then add stringA to another variable stringMessage and output the value of stringMessage.
Eg: Your biggest problems are : Prob1, Prob2, Prob3,
I already have an array called problemArr which gets updated from another function which I haven't included in this snippet of code. (This part works I'm able to demonstrate that the array gets updated).
I've read a few posts on here about Function Scope and hoisting, which I think may have something to do with it. Not sure.
var stringA = ' ';//initialize variable which will form list of problems
var stringMessage ='Your biggest problems are :' + stringA; // Output
var problemArr[]; //Empty array. Gets elements from another function - this part works, I've checked.
//create list of problems
function messageString(){
for(i in problemArr){
stringA = stringA + problemArr[i] + ',';
}
return stringA;
}
problemArrdoes not exist anywhere, start by defining it or passing it to the function.messageString. Why don't you provide a complete example?problemArr?problemArris already defined, why don't you dovar stringMessage ='Your biggest problems are :' + messageString();instead ofvar stringMessage ='Your biggest problems are :' + stringA;? You are setting thestringMessagewith the current value ofstringAwhen you define it andstringMessagewon't change despite you are changing it on your loop.