用户在表格中输入2个数字,输出数字和总和。输出,但不在表格中?

时间:2015-04-19 19:34:47

标签: javascript function html-table

编写一个程序,要求用户输入三个数字。如果所有三个条目都是数字,则输出三个条目中所有数字对的组合的总和。执行添加并输出每对结果的代码应该在函数中,并从main函数调用。在HTML表格中输出答案。应将表及其结束标记添加到HTML页面。表行将与程序一起插入。

我可以让程序主要工作,但它没有输出数字和总和作为表格的一部分,而且我不知道我在哪里/什么&# 39; m失踪?

    //function to output sum of number1 and number2
    function outputSumOfnumber1Andnumber2(number1, number2) {
        var output;
        sum1and2 = number1 + number2;
        output = document.getElementById('outputPart2');
        output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number2 + "</td><td>" + sum1and2 + "</td></tr>";
    }

    function outputSumofnumber2Andnumber3(number2, number3) {
        var output;
        sum2and3 = number2 + number3;
        output = document.getElementById('outputPart2');
        output.innerHTML += "<tr><td>" + number2 + "</td><td>" + number3 + "</td><td>" + sum2and3 + "</td></tr>";
    }

    function outputSumofnumber1Andnumber3(number1, number3) {
        var output;
        sum1and3 = number1 + number3;
        output = document.getElementById('outputPart2');
        output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number3 + "</td><td>" + sum1and3 + "</td></tr></table>";
    }

    function exercise3Part2() {
        // PART 2: YOUR CODE STARTS AFTER THIS LINE
        var number1;
        var number2;
        var number3;
        var output;
        var tableRows;

        number1 = Number(prompt("Enter a number:"));
        number2 = Number(prompt("Enter another number:"));
        number3 = Number(prompt("Enter one more number:"));

        output = document.getElementById('outputPart2');

        output.innerHTML = "<table><tr><th>Pair part 1</th><th>Pair part 2</th><th>Sum</th></tr>";

        outputSumOfnumber1Andnumber2(number1, number2);
        outputSumofnumber2Andnumber3(number2, number3);
        outputSumofnumber1Andnumber3(number1, number3);

    }

尝试2 -

//function to output sum of numbers
function outputSumOfnumbers(number1, number2, number3, rows) {
    var output;
    var rows;

    sum1and2 = number1 + number2;
    sum2and3 = number2 + number3;
    sum1and3 = number1 + number3;
    output = document.getElementById('outputPart2');
    rows = "<tr><td>" + number1 + "</td><td>" + number2 + "</td><td>" + sum1and2 + "</td></tr>";
    rows += "<tr><td>" + number2 + "</td><td>" + number3 + "</td><td>" + sum2and3 + "</td></tr>";
    rows += "<tr><td>" + number1 + "</td><td>" + number3 + "</td><td>" + sum1and3 + "</td></tr>";

}

function exercise3Part2() {
    // PART 2: YOUR CODE STARTS AFTER THIS LINE
    var number1;
    var number2;
    var number3;
    var myTable;
    var rows;

    myTable = document.getElementById('outputPart2');

    number1 = Number(prompt("Enter a number:"));
    number2 = Number(prompt("Enter another number:"));
    number3 = Number(prompt("Enter one more number:"));
outputSumOfnumbers(number1, number2, number3);
    myTable = document.getElementById('outputPart2');



    myTable.innerHTML += rows;

}

2 个答案:

答案 0 :(得分:0)

嗯,你搞砸了,但是我没有修复这个代码,我更喜欢你选择一种更好的表操作方法,这绝对不是你应该这样做的。

查看此文章:http://www.htmlgoodies.com/beyond/css/working_w_tables_using_jquery.html

我相信当你应用里面的内容时,你会做对的。 (:

答案 1 :(得分:0)

我在我的HTML(带有id)中添加了一个开始和结束表标记,并使用了以下代码:

//function to output sum of number1 and number2
function outputSumOfnumber1Andnumber2(number1, number2) {
    var output;
    sum1and2 = number1 + number2;
    output = document.getElementById('outputPart2');
    output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number2 + "</td><td>" + sum1and2 + "</td></tr>";
}

function outputSumofnumber2Andnumber3(number2, number3) {
    var output;
    sum2and3 = number2 + number3;
    output = document.getElementById('outputPart2');
    output.innerHTML += "<tr><td>" + number2 + "</td><td>" + number3 + "</td><td>" + sum2and3 + "</td></tr>";
}

function outputSumofnumber1Andnumber3(number1, number3) {
    var output;
    sum1and3 = number1 + number3;
    output = document.getElementById('outputPart2');
    output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number3 + "</td><td>" + sum1and3 + "</td></tr></table>";
}

function exercise3Part2() {
    // PART 2: YOUR CODE STARTS AFTER THIS LINE
    var number1;
    var number2;
    var number3;
    var output;
    var tableRows;

    number1 = Number(prompt("Enter a number:"));
    number2 = Number(prompt("Enter another number:"));
    number3 = Number(prompt("Enter one more number:"));

    output = document.getElementById('outputPart2');

    output.innerHTML = "<tr><th>Pair part 1</th><th>Pair part 2</th><th>Sum</th></tr>";

    outputSumOfnumber1Andnumber2(number1, number2);
    outputSumofnumber2Andnumber3(number2, number3);
    outputSumofnumber1Andnumber3(number1, number3);

}