如何使用For循环在JavaScript中输出输入数字的三角形

问题描述 投票:0回答:2

你好!

我一直都是这样的任务:

  • 请求用户输入一个号码
  • 检查用户输入是否为空。此外,输入的检查值是一个数字
  • 在HTML文档上写下数字中的三角形,如下所示:

例如。输出:(假设用户输入的数字为10)

您的输入数字是10。

10

11 11

12 12 12

13 13 13 13

14 14 14 14 14

15 15 15 15 15 15

  • 三角形应该有6行。
  • 使用注释解释程序的工作原理
  • 为清晰起见,请遵循缩进。

这是我到目前为止所尝试的:

var input = prompt("Enter a number: ");

if (input.value == '' || input.value == input.defaultValue) {
  alert("Either you entered a NaN or you left an empty field. \nPlease enter some number!");
}

for (input = 10; input <= 15; input++) {
  var a = '';
  for (var j = 10; j <= input; j++) {
    var a = a + '' + input;
  }

  document.writeln(a + "<BR>");

}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Task Write your Own Code</title>

</head>

<body>
  <h1>Task Write your Own Code</h1>


</body>

</html>
  • 首先,即使我输入一个字符串或者没有留下空白的输入字段,我的IF语句也无法正常工作 - 警告信息仍会弹出;
  • 即使在输入字符串或空字段的提示弹出后,document.writeln的结果仍然打印;

请有人帮助我解决这个问题,或者至少告诉我我做错了什么?

谢谢!

javascript for-loop
2个回答
0
投票

查看window.prompt()的文档。

删除.valueinput是价值。

此外,如果input“糟糕”,你不会告诉你的代码不运行。

// Be in a function so that you can return
(function() {

  var input = prompt("Enter a number: ");

  if (!input) {
    alert("Either you entered a NaN or you left an empty field. \nPlease enter some number!");
    // Get of of the function
    return;
  }

  for (input = 10; input <= 15; input++) {
    var a = '';
    for (var j = 10; j <= input; j++) {
      var a = a + '' + input;
    }

    document.writeln(a + "<BR>");

  }
}());

0
投票

|| input.value == input.defaultValue没有任何意义,因为没有input.defaultValue这样的东西,即使有,你只需要检查一个空字符串。此外,input已经是用户的回应,因此不需要.value

您需要在else语句中添加if条件,因为即使没有输入数字,您的代码也会继续执行循环。

此外,document.write()仅在极少数情况下使用,您可以动态地从头开始构建新文档。它不应仅用于更新现有页面的内容。相反,提前准备一个空元素,准备好后,更新该元素的内容。

您的循环配置也有点偏差。

在线查看其他评论:

// Get the user's response, converted to a number
var input = parseInt(prompt("Enter a number: "), 10);

// Get a reference to the waiting output area on the page
var output = document.getElementById("output");

// Check that a number was given
if (!isNaN(input)) {
  // We have a number...
  // You know you need to go 6 times
  for (x = 1; x < 7; x++) {
    var a = '';
    // And then you need to go however many times the outer loop is on
    for (var j = 1; j <= x; j++) {
      a += input + ' ';  // You just need to write out the current input value
    }
    input++;  // Increase the value
    
    // Update the output area on the page
    output.innerHTML += a + "<br>";
  }
} else {
  // We don't have a number:
  alert("Either you entered a NaN or you left an empty field. \nPlease enter some number!");
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Task Write your Own Code</title>

</head>

<body>
  <h1>Task Write your Own Code</h1>

  <div id="output"></div>
</body>

</html>

而且,如果你更多地进入String操作,你会发现你甚至不需要内循环:

// Get the user's response, converted to a number
var input = parseInt(prompt("Enter a number: "), 10);

// Get a reference to the waiting output area on the page
var output = document.getElementById("output");

// Check that a number was given
if (!isNaN(input)) {
  // We have a number...
  // You know you need to go 6 times
  for (x = 1; x < 7; x++) {
    // Set up the string to be written and then just repeat that 
    // however many times the outer loop is currently on.
    output.innerHTML += (input + " ").repeat(x) + "<br>";
    input++;  // Increase the value
  }
} else {
  // We don't have a number:
  alert("Either you entered a NaN or you left an empty field. \nPlease enter some number!");
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Task Write your Own Code</title>

</head>

<body>
  <h1>Task Write your Own Code</h1>

  <div id="output"></div>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.