Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: OR operator not giving expected output

I’m currently challenging myself to make a D&D 5E character sheet from scratch using HTML, CSS, and JavaScript. Currently, I’m merely testing because I’m very new to JavaScript and I’m not too sure how it operates yet. The problem I am having right now is creating if statements with OR operators. I don’t want to create a giant, bloated "if, if else, else" block, I want to streamline it a bit.

Here is my HTML for the dropdown selector for character classes.

<input type="number" id="stat" value="0"> <button id="roll-btn">Roll Stats</button><br><br>
    <select name="class" id="character-class">
        <option value="artificer">Artificer</option>
        <option value="barbarian">Barbarian</option>
        <option value="bard">Bard</option>
        <option value="cleric">Cleric</option>
        <option value="druid">Druid</option>
        <option value="fighter">Fighter</option>
        <option value="monk">Monk</option>
        <option value="paladin">Paladin</option>
        <option value="ranger">Ranger</option>
        <option value="rogue">Rogue</option>
        <option value="sorcerer">Sorcerer</option>
        <option value="warlock">Warlock</option>
        <option value="wizard">Wizard</option>
    </select>

Here is the test version of the javascript that is supposed to read what type of class is selected, and then change values depending on what type of class (what hit dice it uses, saving throw proficiencies, etc.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let characterClass = document.getElementById("character-class").value /* Determines the selected class. Will be set to "artificer" by default */
console.log(characterClass)

if (characterClass === "wizard"||"sorcerer") { /* Wizard and sorcerer both have a hit die value of a D6 */
    console.log("Squishy boi")
} else {
    console.log("Not a wizard/sorcerer")
}

However, when I run this code, it prints "squishy boi" despite being set to "artificer" by default. Am I misunderstanding how || (or) works in JS? Is it not "if characterClass is either wizard or sorcerer, do this, otherwise do this."?

>Solution :

it needs to be if (characterClass === "wizard"|| characterClass === "sorcerer") {

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading