1

my Goal is to be able to create a function that you can give a string as an argument, and be able to make an array that I could add Items to. You can see my attempt here, but it doesn't seem to work. aka, if I want to make a list name GroceryList, it returns GroceryList, but when I want to add an item to it, it says GroceryList is not defined.

function removeInstance(list, item){
  for(var i = 0; i < list.length; i++){
    if(item === list[i]){
      list.splice(i, 1);
      console.log(list);
      break;
    }
  }
}
function makeList(name){
  name = [];
  console.log(name);
  return name;
}
function removeAllItems(list, item){
  for(var i = 0; i < list.length; i++){
    if(item === list[i]){
      list.splice(i, 1);
      i--;
    }
  }
  console.log(list);
}
function addItem(list, item){
    list.push(item);
    console.log(list);
}

any help would be awesome. Thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
michael hall
  • 15
  • 2
  • 7
  • because `name = []` does not use the variable name you pass in... Do you want to make a global variable that holds the array? – epascarello Oct 26 '15 at 23:28
  • function makeList does not do what you think it is doing. It creates a global variable by the name of "name" of the type array. It echos the name object and returns it. One statement would have given you the same result. You might get closer to your goal using the eval statement but I am going to hazard a guess that a better explanation why you wnat to do this is in order. – Sean Phillips Oct 26 '15 at 23:30
  • It is not a global @SeanPhillips – epascarello Oct 26 '15 at 23:31
  • ah good point - but at no point is it naming a variable except for the loal one to the function named "name" – Sean Phillips Oct 26 '15 at 23:33
  • You seem to be reinventing the wheel, here. Check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype for all your JavaScript list needs. – Hypaethral Oct 26 '15 at 23:45
  • Don't try to use dynamically-named variables. Use an object, and the names should be properties of the object. – Barmar Oct 27 '15 at 00:00

2 Answers2

0

Parameters in JavaScript functions are passed by value, not reference; therefore, when you do something like this

var foo = 'bar';

function makeList(name){
  name = [];
  console.log(name);
  return name;
}

makeList(foo);
console.log(foo); // shows 'bar'

you are not modifying the variable which was declared outside the function. Changes to a parameter only affect that local function and not the original variable.

At any rate, I cannot think of any possible scenario where you would need such a makeList function. If you need an array, just create one - there is no need for this!

unpollito
  • 937
  • 1
  • 11
  • 30
0

You need a scope in which you create this array. Suppose you put the JavaScript in an HTML page and you want the array to be in the global scope, then you can create the array as a property of the window object:

function makeList(name){
   window[name] = [];
   return window[name];
}

You can use it like this:

    var arr = makeList("myList");
    myList.push("hello");
    alert('item 0 = ' + myList[0]);
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32