我对正确构建“如果”指令有疑问

我在构建函数时遇到问题。它应该检测用户放入框中的内容是否为整数,并打印正确的评论。问题出现在函数的第二级中,指令“if”应该再次检查图1是否是一个数字(它不会打印任何东西),但是“else if”被完美地读取,“else”也是如此。我的功能有什么问题?请帮忙。


function show() {

  //Variable contains the value of field1 from input html which has been read thanks to onclick

  var figure1 = document.getElementById("field1").value; 

  //Variable contains the value of field2 from input html which has been read thanks to onclick

  var figure2 = document.getElementById("field2").value; 


  //empty until the instruction write in something

  let sign = "";


  //STEP 1

  //loop working until the value of i reach the value of figure2

  for (i = figure1; i <= figure2; i++) 

  {

    if (Number(figure1) && Number(figure2)) {

      //if both figure1 and figure2 are numbers, print the sign as follows - what sign contains from previous iteration + i + ", "

      sign = sign + i + ", "; 

      

    } else {

    

      if (Number(figure1)) {

        //If statements from "if" level above haven't been fulfilled, print "Please, type integer in right-hand box";

        sign = "Proszę wpisać liczbę w prawym polu"; 

        

      } else if (Number(figure2)) {

        //If statements from "if" level above haven't been fulfilled, print "Please, type integer in left-hand box";

        sign = "Proszę wpisać liczbę w lewym polu"; 

        

      } else {

        sign = "Proszę wpisać wartości liczbowe w obu polach";

        // If statements from "if"

        // level above haven 't been fulfilled, print "Please, type integer in both boxes";

      }

    }

  }

  //podmienia wartość diva o id result na wartość zmiennej sign.

  document.getElementById("result").innerHTML = sign; 

}



开满天机
浏览 72回答 2
2回答

犯罪嫌疑人X

无法评论(声誉不够),所以我将以答案的形式写下我的评论。这行代码有 3 个问题:for&nbsp;(i&nbsp;=&nbsp;figure1;&nbsp;i&nbsp;<=&nbsp;figure2;&nbsp;i++)你说这个函数的目的是确保 figure1 和 figure2 都是数字,但是通过把它们放在一个这样的循环中,你已经假设它们是数字。想想看 - 如果值不是数字,您如何增加它们并将它们与for循环进行比较?您假定图 1 始终小于图 2。我有这个假设的基础吗?如果它是假的,那么循环甚至不会执行一次。此外,我不明白为什么需要循环。如果要检查输入是否为整数,则需要循环并更改其值?检查一次应该就足够了。修复这些问题并检查您的问题是否仍然存在。

MMTTMM

没关系<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Document</title>&nbsp; &nbsp; <script type = "text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; function show()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var figure1 = document.getElementById("field1").value; //Zmienna przechowuje wartość pola field1, które zostaje zczytane po wywołaniu funkcji przy użyciu zdarzenia "onclick"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var figure2 = document.getElementById("field2").value; //Zmienna przechowuje wartość pola field2, które zostaje zczytane po wywołaniu funkcji przy użyciu zdarzenia "onclick"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let sign = ""; //zmienna przechowuje pusty ciąg znaków, do momentu nadpisania jej zawartości przez instrukcje funkcji&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Number(figure1) && Number(figure2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i=figure1; i<=figure2; i++) //pętla wykonująca się dopóki wartość figure1 jest mniejsza lub równa figure2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign = sign + i + ", "; //jeżeli obie wartości pobrane z pól field1 i field2 są liczbami, to wykonuj instrukcję pętli for, tj. inkrementuj wartość i od wartości field1 do wartości field2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i=figure1; i>=figure2; i--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign = sign + i + ", "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (Number(figure1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign = "Proszę wpisać wartość liczbową w polu prawym";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (Number(figure2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign = "Proszę wpisać wartość liczbową w polu lewym";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign = "Proszę wpisać wartości liczbowe w polach prawym i lewym";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("result").innerHTML = sign; //podmienia wartość diva o id result na wartość zmiennej sign.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></head><body>&nbsp; &nbsp; <input type="text" id="field1"> <!--W tym polu umieszczamy liczbę z przedziału-->&nbsp; &nbsp; <input type="text" id="field2"> <!--W tym polu umieszczamy liczbę z przedziału-->&nbsp; &nbsp; <input type="submit" value="pokaż" onclick="show()"> <!--Po kliknięciu zostaje wywołana funkcja "show()"-->&nbsp; &nbsp; <div id="result"></div> <!--wnętrze diva podmienimy przy użyciu funkcji "show()"--></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript