1

Okay, I'm just doing a function that moves everything from one list to another, and as soon as that's finished, it spontaneously becomes undefined. There is no good reason for it to do that but what's the bad reason?

Here, look,

function moveList(){//in my real program it actually shuffles the list, this one just transfers the contents
    var listOne=[0, 1, 2, 3];//we have a list
    var listTwo=[];//now we have an empty list
    var length = listOne.length//and now we have the list length
    for (run = 0; run < length; run++) {
        listTwo.push(listOne[0]);//it just copies the first entry from the first list to the second
        listOne.splice(0, 1,)//and deletes it from the first list
    }//this does it for an entry in the list, it's length amount of times
    console.log("listTwo: " + listTwo);//this tells us what the new list now is, and it works
    console.log("listOne: " + listOne);//this tells us what the original list is, which is empty
}
var listOne;
var listTwo;
//these are mandatory, without this, even with the function below script,
//you have Uncaught ReferenceError: list(One and Two) is not defined
moveList();//now we do the function, and then,
console.log("listTwo: " + listTwo);//THE LIST IS NOW UNDEFINED??? What??????

Just why? I literally just defined it in my function. What's wrong with you (literally, I don't see)?

  • 3
    You declared the variable **inside** the function, so it is therefore scoped to that function and not available outside. – Pointy Jun 01 '20 at 18:31
  • The `listTwo` you are printing out at the end of the snippet is not at all the same as the `listTwo` inside of your `moveList` function - they are two separately defined variables – awarrier99 Jun 01 '20 at 18:31
  • 1
    you have to learn about variable scope – Mister Jojo Jun 01 '20 at 18:32
  • Soz I'm a newbie. This is like the third JavaScript I've ever programmed _ So how do you break free of the scope...? _ OK OK I'm trying to read lol – FireyDeath4 Jun 01 '20 at 18:33

2 Answers2

0

You have two separate listTwo variables. One is global, while the other is local to the function moveList. The two occurrences have nothing to do with each other. Same goes for listOne.

So your code is equivalent to the following code, where I have changed the names to be clearer:

function moveList(){//in my real program it actually shuffles the list, this one just transfers the contents
    var localListOne=[0, 1, 2, 3];//we have a list
    var localListTwo=[];//now we have an empty list
    var length = localListOne.length//and now we have the list length
    for (run = 0; run < length; run++) {
        localListTwo.push(localListOne[0]);//it just copies the first entry from the first list to the second
        localListOne.splice(0, 1,)//and deletes it from the first list
    }//this does it for an entry in the list, it's length amount of times
    console.log("listTwo: " + localListTwo);//this tells us what the new list now is, and it works
    console.log("listOne: " + localListOne);//this tells us what the original list is, which is empty
}
var globalListOne;
var globalListTwo;
//these are mandatory, without this, even with the function below script,
//you have Uncaught ReferenceError: list(One and Two) is not defined
moveList();//now we do the function, and then,
console.log("listTwo: " + globalListTwo); // Obviously globalListTwo has never been given a value, so it is undefined.
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
0

The reason is that you initialize TWO lists with the name listTwo in different scopes! One in the global scope, which you don't give it a value, so it stays undefined, and another one inside the function, and that cannot be reached from outside the function as there is another variable with the same name already!

For your code to work you should use the variables inside the function without declaring them.

function moveList(){//in my real program it actually shuffles the list, this one just transfers the contents
    listOne=[0, 1, 2, 3];//we have a list
    listTwo=[];//now we have an empty list
    var length = listOne.length//and now we have the list length
    for (run = 0; run < length; run++) {
        listTwo.push(listOne[0]);//it just copies the first entry from the first list to the second
        listOne.splice(0, 1,)//and deletes it from the first list
    }//this does it for an entry in the list, it's length amount of times
    console.log("listTwo: " + listTwo);//this tells us what the new list now is, and it works
    console.log("listOne: " + listOne);//this tells us what the original list is, which is empty
}
var listOne;
var listTwo;

moveList();//now we do the function, and then,
console.log("listTwo: " + listTwo);//THE LIST IS NOW NOT UNDEFINED ;)

The same applies for listOne variable as well.

Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
  • Okay okay okay, it works, can't believe it was three letters but I couldn't find any answers on Google Search so that might have to be optimised. Also I have time limits for accepting answers for some reason so I'll get to you all in a bit – FireyDeath4 Jun 01 '20 at 18:39