2
I’m trying to make a site where the player can calculate the status of his character automatically to streamline at chip time, character creation and no mistakes.
So I made the following code (using jQuery), depending on the entered value, already automatically calculates the mod (modifier) and the rank of your skill.
However, I’m not able to make it work, only works my first if()
and only the Default that I set in switch()
, I wonder why it’s not working.
$('#str').on('input', function() {
let modificador = $('.str-mod');
let rank = $('.str-rank');
if (this.value == '' || this.value <= 0) {
rank.text('F');
modificador.text('-10 ');
} else {
let modificadorStatusBase = -5;
let modificadorBase = Math.floor(this.value / 2) + modificadorStatusBase;
modificador.text((modificadorBase < 0 ? '' : '+') + modificadorBase + ' ');
switch (this.value) {
case this.value >= 4 && this.value <= 9:
rank.text('D');
break;
case this.value >= 10 && this.value <= 15:
rank.text('C');
break;
case this.value >= 16 && this.value <= 21:
rank.text('B');
default:
rank.text('E');
break;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="div-pontos">
<fieldset>
<legend>Status</legend>
<form class="form-pontos">
<div>
<input type="text" nome="str" id="str" placeholder="Força" value="22" maxlength="3">
<br>
<label for="str">[
<span class="str-mod">+6 </span>
<span class="str-rank">A</span> ] Str (Força)
</label>
</div>
</form>
</fieldset>
</div>
It is worth remembering that by not using the loose comparison, but yes
===
, the result of the expression(valor === (valor >= 10 && valor <= 20))
will never be true if the type ofvalor
is not boolean. In fact, it will be valid only ifvalor = false
, which makes no sense at all in this case - which explains why we always enter thedefault
when usedswitch(valor)
.– Woss