Build a Roman Numeral Converter Project - Build a Roman Numeral Converter

Tell us what’s happening:

Hi all,
I’ve gotten this to work by explicitly calling the .value method in my if statements but was curious as to why it doesn’t work if I do so by throwing it in a variable?

Any help is greatly appreciated! I just want to make sure I understand what’s happening here. Also if I am going about this the wrong way with the code that works please let me know.

Your code so far

/* file: script.js */
const userInput = document.getElementById('number');
const convertBtn = document.getElementById('convert-btn');
const result = document.getElementById('output');
// Created to keep from multiple .value method calls
const userInputValue = userInput.value;

// input validation
// only works for both when I explicitly use .value on both input references
const inputValidation = (input) => {
  if (input === '') {
    result.innerText = 'Please enter a valid number';
    return console.log(parseInt(input));
  } else if (parseInt(input) <= -1) {
    result.innerText = 'Please enter a number greater than or equal to 0';
  }
};

// Does not trigger else if - if I use userInputValue
convertBtn.addEventListener('click', () => {
  inputValidation(userInputValue);
})

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build a Roman Numeral Converter Project - Build a Roman Numeral Converter

Just changed my 2nd if statement as well. I noticed that I did not follow directions on that LOL

when do you think the value of this is determined? can it change?

I think I understand. So because I initialized it as a constant and userInput.value is empty to begin with, it will never update with the new input value. Which is why it only hits the first if statement.

it’s not related to const, it’s that you are capturing the value when the app first load, not when the user did something with the app

1 Like

Ahhh ok I see. Thank you for clearing things up