0

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;

}
7
  • 1
    problemArr does not exist anywhere, start by defining it or passing it to the function. Commented Jul 14, 2015 at 14:08
  • 2
    The code doesn't output anything. You are also not calling messageString. Why don't you provide a complete example? Commented Jul 14, 2015 at 14:08
  • where is problemArr? Commented Jul 14, 2015 at 14:08
  • Is problemArr also a global variable? Where is the array defined? Commented Jul 14, 2015 at 14:10
  • 1
    Assuming your problemArr is already defined, why don't you do var stringMessage ='Your biggest problems are :' + messageString(); instead of var stringMessage ='Your biggest problems are :' + stringA;? You are setting the stringMessage with the current value of stringA when you define it and stringMessage won't change despite you are changing it on your loop. Commented Jul 14, 2015 at 14:15

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

What's wrong with stringA = stringA + problemArr[i] + ',';? Not using += is perfectly fine. The OP certainly does not need to use it.
Why are you writing to the document?
writing just to view the result, @FelixKling I have edited the post
1

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

0

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

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.