Home>

I'm a beginner. I thought about it for about an hour, but I didn't understand, so I'll ask you a question!
What I want to achieve is to start msg1, msg2, msg3, msg4 and num1, num2, num3, num4 when Y: 30 and Z: 5 are inserted, and msg1 ... msg4 and num5 when 40 and 5 are inserted. ... num8 When 50 and 5 are inserted, I want to start and display msg1 ... msg4 and num9 ... num12. I wanted to display it in the table, so I made four variables. I'm sorry for those who commented before making it a table. The input id is ynumber and xnumber.

I got an error when I pressed the calculate button.

Uncaught TypeError: Assignment to constant variable.
    at search_num (tonebar_reference.html: 62)
    at HTMLInputElement.onclick (tonebar_reference.html: 26)
search_num @ tonebar_reference.html: 62
onclick @ tonebar_reference.html: 26
Corresponding source code
<! DOCTYPE html>
<html>
<head>
</head>
<body>
X: 6.35
<br>Y:<input type = "number">
And Z:<input type = "number">
Is<input type = "button" value = "search" onclick = "search_num ()">
<table border = "1">
<tr>
<th></th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
<tr>
<td>Num =</td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
</table>
<script type = "text/javascript">
const ynumber = document.getElementById ("ynumber"). value;
const znumber = document.getElementById ("znumber"). value;
const num1 = 2689.7
const num2 = 3370
const num3 = 14073
const num4 = 15080
const num5 = 1523.8
const num6 = 1919.7
const num7 = 8932.6
const num8 = 10537
const num9 = 978.24
const num10 = 1235.7
const num11 = 5865.8
const num12 = 7232.6
function search_num () {
if (ynumber = 30) {
    if (znumber = 5) {
document.getElementById ("msg1"). innerHTML = num1;
document.getElementById ("msg2"). InnerHTML = num2;
document.getElementById ("msg3"). InnerHTML = num3;
document.getElementById ("msg4"). InnerHTML = num4;
        }
    }
if (ynumber = 40) {
    if (znumber = 5) {
document.getElementById ("msg1"). innerHTML = num5;
document.getElementById ("msg2"). InnerHTML = num6;
document.getElementById ("msg3"). InnerHTML = num7;
document.getElementById ("msg4"). InnerHTML = num8;
        }
    }
if (ynumber = 50) {
    if (znumber = 5) {
document.getElementById ("msg1"). innerHTML = num9;
document.getElementById ("msg2"). InnerHTML = num10;
document.getElementById ("msg3"). InnerHTML = num11;
document.getElementById ("msg4"). InnerHTML = num12;
        }
    }
}
</script>
</body>
</html>
Javascript, html, files are .html
What I tried

Change the order of the three if statements: no change
I wondered if there was something missing such as {}, but I don't think it is.
I changed the order of znumber and ynumber, but there is no change.

Supplementary information (FW/tool version, etc.)

Visual Studio 2019 version 16.8.1
It is a free version of.

  • Answer # 1

    VariableynumberWhenznumberIs not defined

    if (znumber = 5) {If it is written like this, the conditional statement will beznumber5 is assigned to the variable, and the condition is always true.if (znumber === 5) {Is correct

Related articles