I'm working on a function where I check a value that is passed in, and then add it to an array. What I have isn't working, I just get an empty array:
let category = 'client';
function builtArray() {
let catArray = [];
if (category === 'client') {
catArray.push('client');
}
console.log(catArray);
}
If I understand your question correctly,
let category = 'client';
function builtArray() {
let catArray = [];
if (category === 'client') {
catArray.push('client');
}
console.log(catArray);
}
should be
let category = 'client';
function builtArray(cat) {
let catArray = [];
if (cat === 'client') {
catArray.push(cat);
}
console.log(catArray); // => ['client']
}
builtArray(category);
You have to "invoke" the function for it to run. This means calling it with ()
, that's what I've done at the end.
You pass "category" in as an argument to the function, the function renames what you've passed in (as cat
), it then checks to see if that variable is equal to the string client
, if it is, it inserts it into catArray
.