Integer comparison failing all possible conditional statements

Viewed 21

Have been messing with this for awhile and cannot for the life of me figure out why assetQualifier[ii][0] never passes any of the conditional tests except "else".

We can see the console log that the variable type is, indeed, a number. So it is a number variable being compared with a number variable, within the correct range of numbers, and yet it fails to evaluate as "true" for any of the possible numbers.

I ran a series of tests in the console log, as well, and all of those tests resolve to false, even after I force the variable into a Number - which is redundant, I realize, but just to show that I covered all possible bases, in case there was some error in logic.

            var rollFood = Math.floor(Math.random()*10); 
            var rollShelter = Math.floor(Math.random()*10); 
            var rollTerritory = Math.floor(Math.random()*10);
            var rollMaterials = Math.floor(Math.random()*10); 
            var rollTools = Math.floor(Math.random()*10);
            var rollGoods = Math.floor(Math.random()*10);
            var rollCurrency = Math.floor(Math.random()*10);
            var rollWater = Math.floor(Math.random()*10); 

            var assetQualifier = [[rollFood,"test"],[rollShelter,""],[rollTerritory,""],[rollMaterials,""],[rollTools,""],[rollGoods,""],[rollCurrency,""],[rollWater,""]];
            console.log("assetQualifier.length: "+assetQualifier.length);
            console.log("assetQualifier[0][1]: "+assetQualifier[0][1]);
            console.log(typeof assetQualifier[0][0]); //this is a number, yes?
            console.log(typeof Number(assetQualifier[0][0])); //and that is DEFINITELY a number...

            var ii=0; //counter
            while(ii<assetQualifier.length){
                console.log("assetQualifier[ii][0]>0: "+Number(assetQualifier[ii][0])>0);
                console.log("assetQualifier[ii][0]==0: "+Number(assetQualifier[ii][0])==0);
                console.log("assetQualifier[ii][0]===0: "+assetQualifier[ii][0]===0);
                console.log("assetQualifier[ii][0]==0: "+Number(assetQualifier[ii][0])==5);
                console.log("assetQualifier[ii][0]==0: "+Number(assetQualifier[ii][0])<5);
                if(assetQualifier[ii][0]===0){assetQualifier[ii][1]="is totally destitute in terms of"}
                if(assetQualifier[ii][0]===1){assetQualifier[ii][1]="is cripplingly deficient in"}
                if(assetQualifier[ii][0]===2){assetQualifier[ii][1]="has dodgy access to"}
                if(assetQualifier[ii][0]===3){assetQualifier[ii][1]="has some limited access to"}
                if(assetQualifier[ii][0]===4){assetQualifier[ii][1]="has modest access to"}
                if(assetQualifier[ii][0]===5){assetQualifier[ii][1]="is equal to his/her peers in terms of"}
                if(assetQualifier[ii][0]===6){assetQualifier[ii][1]="is faring a little better than average in terms of"}
                if(assetQualifier[ii][0]===7){assetQualifier[ii][1]="could fairly be regarded as privileged in terms of"}
                if(assetQualifier[ii][0]===8){assetQualifier[ii][1]="possesses a varied expanse of"}
                if(assetQualifier[ii][0]===9){assetQualifier[ii][1]="is up to his/her ears in"}
                if(assetQualifier[ii][0]===10){assetQualifier[ii][1]="is laughably subsumed by sheer glittering excess when it comes to"}
                else{assetQualifier[ii][1]="has resources as of yet unknown in terms of"};
                console.log("assetQualifier[ii][1]: "+assetQualifier[ii][1]);
                ii++;
            }

The outcome of this is always the "else", no matter what.

I think this snippet of code is sufficient to show what I'm going for, but in case it's relevant, the outcome is going to be output as a string in a captured DIV.

Any words of wisdom would be greatly appreciated.

1 Answers

Your code works to some degree, just log assetQualifier after the while loop to check. One issue is the last part:

if(assetQualifier[ii][0]===10)
  // code
else 
  // code 

this else is only paired with the if which will never be 10 since Math.floor(Math.random() * 10)) will never be 10

Javascript creates pseudo-random numbers with the function Math.random(). This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.

Javascript Math Random

Personally, I would rewrite the logic like this:

// helper function which rolls 0 - 10
const rollUpTo10 = () => Math.floor(Math.random() * 11)

// array of all assets
const assets = [
  "is totally destitute in terms of",
  "is cripplingly deficient in",
  "has dodgy access to","has some limited access to",
  "has modest access to","is equal to his/her peers in terms of",
  "is faring a little better than average in terms of",
  "could fairly be regarded as privileged in terms of",
  "possesses a varied expanse of",
  "is up to his/her ears in",
  "is laughably subsumed by sheer glittering excess when it comes to",
  "has resources as of yet unknown in terms of"
]

// iterate the assets rolling for each asset and if the roll is less
// than the total length (8) return an array with [rollValue, asset?]
const assetQuantifier = assets.map((item, index) => {
  const roll = rollUpTo10()
  const text = roll < assets.length ? item : ""
  return [roll, text]
})

// output to screen
console.log(assetQuantifier)

Related