There is such code:

function turnBot(){ var min = 15; for(var i = 0; i < cardN[0].length; i++){ var data = explode(".", cardN[0][i]); var number = data[0]; var mast = data[1]; console.log('number' + number + 'min:' + min); if(number < min){ min = number; console.log('MIN:' + min); var cardName = number + mast; } } clearCard(cardName); addCardForTable(cardName); enabledTurnPlayer(); } 

I try to fulfill the if(number < min) condition in the function, but it is not executed:

console log

Screen Code:

function

Question: why the condition is not satisfied?

  • That's right, '9' <'10'. - Pavel Mayorov
  • yes, but the condition does not work, thanks to KEP, I already know that '9' < '10 ' - Pasha Talyanchuk
  • four
    Neither you nor @PavelMayorov are both right, because "9" is greater than "10" because the comparison is in lines, not as numbers. Bring everything to numbers explicitly through parseInt and there will be no problems. - Alex Krass
  • Please add code in the body of the question as text, not an image. To do this, use the " edit " link located under the question marks. - Nicolas Chabanovsky

1 answer 1

You may have a problem with the types. The error is that you compare strings, which is an error, because:

 "8"<"10" false 8<10 true 

Try explicitly converting values ​​to int:

 ... if (parseInt(num) < parseInt(min)) { ... 

Ps. I also recommend that you use debager and breakpoints (available in modern browsers out of the box), or make the conclusion more beautiful. An example of beautiful output:

 console.log("Number:", number, "; Min:", min) 

With this output, you can explicitly trace the type of the variable, and not lose it during conversions.

  • Thank you, forgot about parseInt - Pasha Talyanchuk