I have written some code that checks if an array exists and if not, creates one called window.list
It then checks if the array contains a 2 or a 3 value and if not adds them to the list.
if they are within the list already it outputs "Value is already in the list!" then breaks out of the function.
When I execute the code it only recognises the first clause of inArray that checks for a 2 value. 3 does not function. I would like the ability to add more values in the future. I'm not sure this is the most efficient way to write this query.
if (typeof window.list == "undefined" || !(window.list instanceof Array)) {
window.list = new Array;
}
if ($.inArray(2, window.list) == -1 || $.inArray(3, window.list) == -1) {
window.list.push(presentationId);
} else {
console.log("Value is already in the list!")
return;
}
presentationId?if ($.inArray(presentationId, window.list) === -1) { window.list.push(presentationId) } ...This way you don't need to care whatpresentationIdcould be in the future (better for maintenance).presentationIdis2, you don't want to add it twice just because3isn't in the array. Though without knowing how this code is being called it's difficult to know what the problem they're experiencing is being caused by; if$.inArray(2, window.list) == -1is false it should check for the presence of3as well.