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;
}
3 Answers 3
You need to define your array it is missng, then call the function you have created as follows.
var stringA = ' ';//initialize variable which will form list of problems
var problemArr = ['1','2','3']; // <<<<<<<<< define the array
//create list of problems
function messageString(){
for(i in problemArr){
stringA += problemArr[i] ;
// do not add a comma after the last array item
if(i < problemArr.length - 1)
{
stringA += ',';
}
}
}
messageString(); // <<<<<<<<< call the function
var stringMessage ='Your biggest problems are :' + stringA; // Output
document.write(stringMessage);
Edit
To match your case, call the function to create the message string and fill stringA, then set it to the final output afterward var stringMessage ='Your biggest problems are :' + stringA; // Output
3 Comments
stringA = stringA + problemArr[i] + ',';? Not using += is perfectly fine. The OP certainly does not need to use it.trying to replace a string with a string, strings always have to be a new variable, so rather than stringA = stringA you'd need newString = stringA + ","
i've refactored your code, and this will do what you want it to do:
(function () {
var stringA;
var stringMessage = "Your biggest problems are : ";
var problemArr = [1, 2, 3, 4, 5, 6];
for (var i = 0; i < problemArr.length; i++) {
stringA = problemArr[i] + ",";
stringMessage += stringA;
}
alert(stringMessage);
})()
the "+=" operator appends something to an existing object
e.g 1 += 2 would equal 3
e.g "hello" += "world" would equal "helloworld"
Comments
Looks like you're just trying to add the content of an array to a string variable. Why don't you just do it like this:
var problemArr = ["99 problems","but a b****","ain't one"];
//or just use the array you created before
var problemsAsString = problemArr.join(", ");
//converts the array to a string and puts a ", " between each element
var stringMessage = 'Your biggest problems are : ' + problemsAsString;
alert(stringMessage); //output, or do whatever you want with it
Comments
Explore related questions
See similar questions with these tags.
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.