Help with an if function

Tell us what’s happening:

Alright, I give up. After tinkering with my wizard function on and off for a week now I truly do not understand why my else functions don’t work properly. I skimmed this down from the original function to illustrate the issue.

The else if statement is completely sporadic. If I use “else if (loopNum = 5)” this will trigger that condition but break the next one. If I use “else if (loopNum === 5” this will break the condition but fix the next one.

What’s most baffling to me is that with (loopNum = 5) any value over 5 triggers the condition. Have mercy on my brain someone, the more I tinker with this the less sense it makes.

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

const input = document.getElementById("number");

const output = document.getElementById("output");

const button = document.getElementById("convert-btn");

const num =["I", "V", "X", "L", "C", "D", "M"]



const convert = () => {
  let preroman = String(input.value);
  let romArr = preroman.split("").map((preroman) => {
  return Number(preroman)})
  console.log(preroman) 
  return romArr;
  
  };
  

//const romArrComp = convert()

const wizard = () => {
  let loopNum = convert();
  let repeat = 0 ;
  let romString = "";
  let single = "I"
   if (loopNum <= 4) {
  while (loopNum <= 4 && loopNum >    repeat) {
    romString += "I";
    repeat++;
    console.log(romString);
    } 
    output.innerHTML = romString
   } else if (loopNum = 5) { 
     romString = "V"
     output.innerHTML = romString;
   }else if (loopNum >= 6 || loopNum < 9){ romString = "test"
   output.innerHTML = romString;}
}


   
  



const check = () => {if (isNaN(number.value)) {
  
  output.innerHTML= ("Please enter a valid number")}
  else if (number.value < 0) {output.innerHTML="Please enter a number greater than or equal to 1"}
  else if (number.value > 4000) {output.innerHTML="Please enter a number less than or equal to 3999"}
  else (convert())

}


const choice = () => {if (input.value <= 0 || input.value >= 3999){check()}

else if (input.value >= 1 || input.value <= 3998) {wizard()}

}








button.addEventListener("click", choice)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0

Challenge Information:

Learn Basic JavaScript by Building a Role Playing Game - Step 105

Hi!

Back to basics! If statements and other comparator statements should (almost) always be enclosed in parentheses. Never bound them in quotes, other brackets, or mixes.
Second, you will ALWAYS need at least 2 equal signs to test for equality, although 3 is preferred.
Third, a comparator function can only ever do that one thing. It can’t directly set a variable value or increment a variable.

Hope this helps! I see at least three comparator errors you need to fix.

2 Likes

Thanks for your feedback, I cleaned up the wizard function but I still don’t understand why the else if statements only seem to trigger when using =< or =>. Here is what I have now. I can see that in cases where loopNum is indeed 5 or 9, the correct else if statement is not triggered. The else if’s including the “while” statements are markedly more complex but seem to run fine. I would think an else if looking for a single condition like “else if (loopNum ===5)” is pretty straight forward but it refuses to trigger. I’m really baffled…What have I done wrong? Here’s the full function as it stands now:

const wizard = () => {
  let loopNum = convert();
  let repeat = 0 ;
  let repeat2 = 6;
  let romString = "";
  let single = "";
   if (loopNum <= 4) {
  while (loopNum <= 4 && loopNum >    repeat) {
    romString += "I";
    repeat++;
    output.innerHTML = romString;
  }} 
    else if (loopNum === 5) { 
     romString = "V"
     output.innerHTML = romString
   } else if (loopNum >= 6 || loopNum <= 8) 
    
   { while (loopNum <= 8 && loopNum >= repeat2) 
   
   { 
     romString = "V";
     single +="I";
      repeat2++;
      output.innerHTML = romString + single}
      }
 else if (loopNum === 9) {
  romString = "IX"
  output.innerHTML = romString
   }}

Thanks for any additional advice. It’s driving me nuts…

Hi Benjamin. Assuming you haven’t changed your convert code, it looks like you are passing an array back to your wizard function. So you are trying to compare an array in your if statements and while statements. So the question is, which value of the array are you comparing? Try being more specific with your loopnum variable (which array value).

Hi Daniel, thank you for getting back to me again. That is correct, the intention of having the convert function return an array is so that an additional function can then run the wizard function on a specific object in the array, either 0,1,2 or 3 depending on other conditions I haven’t defined yet. Your suggestion on targeting a single object in the array worked on the number 5 with the below code however, it still refuses to run the “else if” statement on 9. I’m not entirely clear on why specifying the object in the array changes the outcome for the number 5 either. Any ideas?

const wizard = () => {
 // console.log(typeof convert())
  let loopNum = convert()[0];
  let repeat = 0 ;
  let repeat2 = 6;
  let romString = "";
  let single = "";
   if (loopNum <= 4) {
  while (loopNum <= 4 && loopNum > repeat) {
    romString += "I";
    repeat++;
    output.innerHTML = romString;
  }} 
    else if (loopNum === 5) { 
     romString = "V"
     output.innerHTML = romString
   } else if (loopNum >= 6 || loopNum <= 8) 
    
   { while (loopNum <= 8 && loopNum >= repeat2) 
   
   { 
     romString = "V";
     single +="I";
      repeat2++;
      output.innerHTML = romString + single}
      }
 else if (loopNum === 9) {
  romString = "IX"
  output.innerHTML = romString
   }
   else console.log("x")};

It would probably help if you used some sort of formatter so your use of whitespace and braces was consistent and easier to read.

You’re getting closer. If you want to know why “9” won’t work, take a closer look at the else if before it (after the “5” else if).

Aha, got it, I changed it from an OR to an & statement which makes much more sense. Any idea why 9 wouldn’t just be passed to the next “else if” statement anyway though, as it wouldn’t have qualified for any of the “else if” triggers as it was.

Ahh, nevermind, of course the previous “else if” statement caught it as 9 is indeed greater than 6. Thank you for all the help

Except that it DID qualify for that statement:
else if (loopNum >= 6 || loopNum <= 8)

This if statement capture all numbers. 9 is captured by the first one because it is greater than 6. Even if the while loop inside this particular “if” doesn’t do anything, the else if statements have completed and skips any remaining else if and else statements.