首页 > 解决方案 > 努力学习乘法

问题描述

问题是,当我输入正确答案时,文本不会变成绿色,而当它不正确时,它不会变成红色。我认为有一个问题是我没有得到输入值,但可以找到解决方法。

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

解决方案


您对该函数的调用未传递任何参数


推荐阅读