Javascript循环 - 无法想出这个

时间:2015-07-04 22:58:41

标签: javascript loops

我已经尝试了所有想到的东西,但是这个循环没有运气(虽然我对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>

1 个答案:

答案 0 :(得分:1)

已移除(拆分下方):if(input=="")

添加(在分割之上):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>");
}

因为取消的prompt()的返回值取决于浏览器。在大多数浏览器中,返回值为null。但是,一些非常古老的浏览器(例如IE的早期版本)用于返回''(空字符串)。

因此,不要使用类似if (input != '' && input != null)的内容,只需使用true或false。

用户按OK,但输入字段为空input === ""

用户输入内容并点击确定(或输入)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)
            });
}