簡體   English   中英

Javascript循環-無法解決這一問題

[英]Javascript Loop - can't figure this one out

我已經嘗試了所有讓我望而卻步的方法,但是此循環並沒有帶來任何運氣(盡管我對javascript很陌生)。
提示應提出問題,直到沒有輸入任何內容。 到那時,所有先前輸入的“結果”都應被處理。 結果應該看起來像(如果輸入第一個提示:'CS A 4',第二個'BB B 3',第三個'CC C 3'..):....僅在第n個提示沒有輸入后才顯示

COURSE GRADE HOURS  
CS     A     4  
BB     B     3  
CC     C     3 

 <!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>gpa.html</title>
</head>
<script type="text/javascript">
    function getData(){
        var input=0,
            results=[];

        while(input!==""){input = prompt("Enter course name, grade and credit hours (e.g., 'CS3520 A 4' or click OK with no data to terminate.");
        input = input.split(" ");
        if(input==""){
        results.push({
            course: input[0].trim(),
            grade: input[1].trim(),
            creditHours: parseInt(input[2], 10)
        });}}
        return results;         
    }

    var app = function() {
        var result, results,
        COLUMN_SEPARATOR = "\t\t";

        document.writeln("<pre>");
        results = getData();

        document.writeln("COURSE" + COLUMN_SEPARATOR + "GRADE" + COLUMN_SEPARATOR + "HOURS");
        for (var i = 0, j = results.length; i < j; i++) {
            result = results[i];

            document.writeln(result.course + COLUMN_SEPARATOR + result.grade + COLUMN_SEPARATOR + result.creditHours);
        }   

        document.writeln();
        computeGPA(results);
        document.writeln("</pre>");    
    }

window.onload = app;

</script>
<body>
</body>
</html>

刪除(在拆分下方): if(input=="")

添加(在拆分上方): if (input === "") { break; } if (input === "") { break; }

應該這樣做:

function getData() {
    var input = 0,
        results = [];

    while (input !== "") {
        input = prompt("Enter course name, grade and credit hours (e.g., 'CS3520 A 4' or click OK with no data to terminate.");
        if (input === "") { break; }
        input = input.split(" ");

        results.push({
            course: input[0].trim(),
            grade: input[1].trim(),
            creditHours: parseInt(input[2], 10)
        });

    }
    return results;
}



var app = function () {
    var result, results,
    COLUMN_SEPARATOR = "\t\t";

    document.writeln("<pre>");
    results = getData();

    document.writeln("COURSE" + COLUMN_SEPARATOR + "GRADE" + COLUMN_SEPARATOR + "HOURS");
    for (var i = 0, j = results.length; i < j; i++) {
        result = results[i];

        document.writeln(result.course + COLUMN_SEPARATOR + result.grade + COLUMN_SEPARATOR + result.creditHours);
    }

    document.writeln();
    document.writeln("</pre>");
}

但是我認為這將是一個更好的解決方案:

function getData() {
    var input = true,
        results = [];

    while (input) {
        input = prompt("Enter course name, grade and credit hours (e.g., 'CS3520 A 4' or click OK with no data to terminate.");
        if (!input) { break; }
        input = input.split(" ");

        results.push({
            course: input[0].trim(),
            grade: input[1].trim(),
            creditHours: parseInt(input[2], 10)
        });

    }
    return results;
}

var app = function () {
    var result, results,
    COLUMN_SEPARATOR = "\t\t";

    document.writeln("<pre>");
    results = getData();

    document.writeln("COURSE" + COLUMN_SEPARATOR + "GRADE" + COLUMN_SEPARATOR + "HOURS");
    for (var i = 0, j = results.length; i < j; i++) {
        result = results[i];

        document.writeln(result.course + COLUMN_SEPARATOR + result.grade + COLUMN_SEPARATOR + result.creditHours);
    }

    document.writeln();
    document.writeln("</pre>");
}

因為取消的hint()的返回值取決於瀏覽器。 在大多數瀏覽器中,返回值為null。 但是,某些非常舊的瀏覽器(例如IE的早期版本)曾經返回”(空字符串)。

因此,不要使用諸如if (input != '' && input != null) ,而應使用true或false。

用戶按下OK,但輸入字段為空input === ""

用戶鍵入了一些內容,然后按OK(或輸入) input == true

用戶按下CANCEL input == nullinput == ""

更新

關於textarea ,嘗試這樣的操作(我沒有測試過):

textareaContentByLines = textareaContent.split("\n");

for(index = 0; index < textareaContentByLines.length; index++){
    input = textareaContentByLines.split(" ");
    results.push({
                course: textareaContentByLines[index][0].trim(),
                grade: textareaContentByLines[index][1].trim(),
                creditHours: parseInt(textareaContentByLines[index][2], 10)
            });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM