I am trying to figure out what I have done wrong in this code. I’m trying to create a question that will show the user how equal and not equal works in JS, where they can enter two values and pick an operator from a drop down menu. The console log has an error that says: Uncaught SyntaxError: expected expression, got '='final.js:21:94
. It looks like I possibly cannot put =
, ==
, or ===
operators into ${}
?
function compareAnswer() {
let numberInput = parseInt(document.getElementById("value1").value);
let stringInput = (document.getElementById("value2").value);
let operator = document.getElementById("operator");
let answer = document.getElementById("answer");
let result;
if (operator.value == "=") {
result = `Value 1 was ${numberInput}, and is now ${numberInput = stringInput}.`;
} else if (operator.value == "==") {
result = `Are ${numberInput} and ${stringInput} equal? ${numberInput == stringInput}.`;
} else if (operator.value == "===") {
result = `Are ${numberInput} and ${stringInput} strict equal? ${numberInput === stringInput}.`;
} else if (operator.value == "not=") {
result = `Are ${numberInput} and ${stringInput} and has not changed ${numberInput != stringInput}.`;
} else if (operator.value == "not==") {
result = `Are ${numberInput} and ${stringInput} not equal? ${numberInput !== stringInput}.`;
} else if (operator.value == "not===") {
result = `Are ${numberInput} and ${stringInput} not strict equal? ${numberInput !=== stringInput}.`;
} else {
default = "try again."
}
answer.textContent = result;
console.log(`working`);
}
document.getElementById("compareButton").addEventListener("click", compareAnswer);
<div id="wrapper">
<div id="question6">
<h2>Question 6</h2>
<h3>Create a value that equals true with one of the operators.</h3>
<lable for="value">Value 1 </lable><input type="number" id="value1">
<br>
<br>
<lable for="value">Value 2 </lable><input type="text" id="value2">
<br>
<select id="operator">
<option value="="> = </option>
<option value="=="> == </option>
<option value="==="> == </option>
<option value="not="> != </option>
<option value="not=="> !== </option>
<option value="not==="> !=== </option>
</select>
<br>
<button id="compareButton">Equals</button>
<p id="answer">Answer</p>
</div>
</div>
2