Reputation: 1
I have some code for an object constructor of the form:
function objectConstructor(item, r) {
//Pulls a string from elsewhere
this.name = Object.ObjectOne[item].name;
this.amount = Object.ObjectOne[item].amount;
this.buttonID = pickButton(r);
this.itemInitialCost = [];
this.itemPriceIncrease = [];
//This is the problematic area.
if(this.name != "valueOne" || this.name != "valueTwo" || this.name != "valueThree" || this.name != "valueFour" || [...]) {
for(var i = 0; i < Object.ObjectTwo[this.buttonID].cost.length; i++) {
this.itemInitialCost.push(Object.ObjectTwo[this.buttonID].cost[i].initialCost;
this.itemPriceIncrease.push(Object.ObjectTwo[this.buttonID].cost[i].costIncrease;
}
} else {
this.itemInitialCost.push("none");
this.itemPriceIncrase.push("none");
}
}
function pickButton(r) {
//The Function
}
var array = [Long List];
for(var i = 0; i < array.length; i++) {
item = array[i];
items[item] = new objectConstructor(item, i);
console.log("Created: " + item);
}
console.log(items);
The first 58 (of 67) item objects are created and assigned values just fine. All items after the 58th don't have a buttonID, and therefore would throw errors when they tried to get their cost values. To prevent this, I have the if statement set to assign those items (based on the item name) the value of "none" for cost. However, they seem to be ignoring the conditions in the if statement and going straight to the for loop that would set a normal items costs. They aren't going to the else statement, and are thereby causing my script to crash.
Why are they, apparently, ignoring the if statement's conditions?
Upvotes: 0
Views: 1133
Reputation: 1397
You want to use &&
instead of ||
, as the latter (called OR
operator) only needs to satisfy one condition to evaluate to true (the former needs all). And If you have an array of those values and you want to make sure none of them matches this.name
, you can use the following instead of typing all the names out:
if(arrayOfValues.indexOf(this.name)<0){
//assign special values
}
Upvotes: 0
Reputation: 5292
Any value for this.name
will evaluate to true for this:
this.name != "valueOne" || this.name != "valueTwo"
You probably want a &&
between those conditions.
Also, maybe it would make more sense to see if this
has some buttonID
rather than checking the name.
Upvotes: 1