如何在方程式之前显示数字?

时间:2016-04-22 01:40:42

标签: javascript

如何修改此代码,以便在打印方程式时,会在它们之前显示一个数字。例如,

2×+ 1 = 12

-x + 2 = 5

这是我需要帮助修改的代码。



function getrandomnumber(min, max, notthese) {
  num = min + Math.floor((max - min + 1) * Math.random());
  return (num);
}

function getoption(s, ch, num) {
  var a = s.split(ch)
  return a[num - 1];
}

function getequation() {
  var num1 = getrandomnumber(20, -20, "");
  var num2 = getrandomnumber(20, -20, "");
  var num3 = getrandomnumber(20, -20, "");
  var num4 = getrandomnumber(20, -20, "");
  var letter = getoption("x,y,z,w", ",", getrandomnumber(1, 4));

  var str = "" + num1 + "" + letter + "+" + num2 + "=" + num3 + "" + letter + "+" + num4 + ""
  var total = (num4 - num2) / (num1 - num3);
  return (str + " (" + total + ")");

}

for (var i = 1; i <= 20; i++)


{

  document.write(getequation() + "<br>")

}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

将迭代器i添加到document.write(getequation()+"<br>")

function getrandomnumber(min, max, notthese) {
  num = min + Math.floor((max - min + 1) * Math.random());
  return (num);
}
function getoption(s, ch, num) {
  var a = s.split(ch)
  return a[num - 1];
}
function getequation() {
  var num1 = getrandomnumber(20, -20, "");
  var num2 = getrandomnumber(20, -20, "");
  var num3 = getrandomnumber(20, -20, "");
  var num4 = getrandomnumber(20, -20, "");
  var letter = getoption("x,y,z,w", ",", getrandomnumber(1, 4));
  var str = "" + num1 + "" + letter + "+" + num2 + "=" + num3 + "" + letter + "+" + num4 + ""
  var total = (num4 - num2) / (num1 - num3);
  return (str + " (" + total + ")");
}
for (var i = 1; i <= 20; i++) {
  document.write("<span>" + i + ": </span>" + getequation() + "<br />")
}
body {
  font: 12px arial;
}
span {
  color: red
}

答案 1 :(得分:0)

我明白了!

function getrandomnumber(min, max, notthese) {
  num = min + Math.floor((max - min + 1) * Math.random());
  return (num);
}

function getoption(s, ch, num) {
  var a = s.split(ch)
  return a[num - 1];
}

function getequation() {
  var num1 = getrandomnumber(20, -20, "");
  var num2 = getrandomnumber(20, -20, "");
  var num3 = getrandomnumber(20, -20, "");
  var num4 = getrandomnumber(20, -20, "");
  var letter = getoption("x,y,z,w", ",", getrandomnumber(1, 4));

  var str = "" + i + ")" + num1 + "" + letter + "+" + num2 + "=" + num3 + "" + letter + "+" + num4 + ""
  var total = (num4 - num2) / (num1 - num3);
  return (str + " (" + total + ")");

}

for (var i = 1; i <= 20; i++)


{

  document.write(getequation() + "<br>")

}