Проблема в том, что когда я ввожу правильный ответ, текст не становится зеленым, а когда он неправильный, он не становится красным. Я думаю, что есть проблема, что я не получаю входное значение, но могу найти для него исправление.
var firstArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var secondArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var first = firstArray[Math.floor(Math.random() * firstArray.length)];
var second = secondArray[Math.floor(Math.random() * secondArray.length)];
var sum = firstArray[first - 1] * secondArray[second - 1];
function fn1(sum, n1) {
var n1 = document.getElementById("n1").value;
if (sum < n1) {
document.getElementById("pp").style.background = "green";
} else if (sum > n1) {
document.getElementById("p").style.background = "red";
} else {
document.getElementById("p").style.background = "blue";
document.getElementById("pp").style.background = "blue";
}
};
document.getElementById("p").innerHTML = first;
document.getElementById("pp").innerHTML = second;
document.getElementById("a").innerHTML = first;
document.getElementById("b").innerHTML = second;
document.getElementById("sum").innerHTML = sum;<h1>
<a id = "a"></a> *
<a id = "b"></a> = <input type = "text" id = "n1" name = "txt"></h1>
<button onclick = "fn1()" id = "btn1">Clcik me</button>
<p id = "p"></p>
<p id = "pp"></p>
<p id = "sum"></p>
<p id = "test"></p>


![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Ваш вызов функции не передает никаких аргументов
ваша функция fn1() не принимает аргументов
var firstArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var secondArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var first= firstArray[Math.floor(Math.random() * firstArray.length)];
var second= secondArray[Math.floor(Math.random() * secondArray.length)];
var sum = firstArray[first- 1] * secondArray[second - 1];
function fn1() {
var n1 = document.getElementById("n1").value;
if (sum < n1) {
document.getElementById("pp").style.background = "green";
} else if (sum > n1) {
document.getElementById("p").style.background = "red";
} else {
document.getElementById("p").style.background = "blue";
document.getElementById("pp").style.background = "blue";
}
};
document.getElementById("p").innerHTML = first;
document.getElementById("pp").innerHTML = second;
document.getElementById("a").innerHTML = first;
document.getElementById("b").innerHTML = second;
document.getElementById("sum").innerHTML = sum;<h1>Learn to Multiply</h1>
<h1> <a id = "a"></a> * <a id = "b"></a> = <input type = "text" id = "n1" name = "txt"></h1>
<button onclick = "fn1()" id = "btn1">Clcik me</button>
<p id = "p"></p>
<p id = "pp"></p>
<p id = "sum"></p>
<p id = "test"></p>Измените на это:
// first remove the parameters
function fn1() {
var n1 = document.getElementById("n1").value;
// I think you want green when is equal
if (sum == n1) {
document.getElementById("pp").style.background = "green";
} else if (sum > n1) {
document.getElementById("p").style.background = "red";
} else {
document.getElementById("p").style.background = "blue";
document.getElementById("pp").style.background = "blue";
}
};
n1 является и параметром, и объявленной переменной.