I want to create dynamic variables based on the arguments of a function but am having some trouble. I am trying to create a function that
1) takes in a genre name, the parameter,
2) turn it into an object,
3) add relevant info, and
4) push it onto an empty array, called bookshelf.
However, my problem is, when I assign an argument genre = new Object();, the name of the parameter genre is name of the object not the argument I give it. So if I put in a genre 'Romance', the object in the bookshelf array is genre not 'Romance'. This is a problem because, I need the array to house different genre names...
var bookshelf = [];
//Input: genre = 'Romance'
function addGenre (genre) {
var genre = new Object ();
genre.someInfo = true;
bookshelf.push(genre);
return bookshelf;
//Ideal output: Romance {someInfo: true}
//Actual output: genre {someInfo: true}
}
//I attempted to use string literals, but I think that's illegal...
function addGenre (genre) {
var `${genre}` = new Object ();
genre.someInfo = true;
bookshelf.push(genre);
return bookshelf;
}
Have a great week :)