Maurice Greenland
Maurice Greenland

Reputation: 315

Javascript: "undefined" value for variable

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;

}

Upvotes: 0

Views: 79

Answers (3)

Gweaths
Gweaths

Reputation: 85

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"

Upvotes: 1

Patric
Patric

Reputation: 1627

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

Upvotes: 0

KAD
KAD

Reputation: 11112

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

Upvotes: 1

Related Questions