1

I already have a table being created in javascript. It is based off user input and will check to make sure the value entered is a number. But how do I ALSO make it check to make sure the values entered are higher then 0 and less then 10

<html>
<head>
    <title>Homework 1</title>

    <script language="javascript" type="text/javascript">
    function doWork(){
    var rows = document.getElementById("input1").value;
    var columns = document.getElementById("input2").value;
    //alert(rows);
    //alert(columns);
    if(isNaN(rows) == true || isNaN(columns) == true){
        document.getElementById('tablePlacement').innerHTML = "Input must be integer";
    }
    else{
    var htmlInput = "";
    htmlInput += "<table border='1'>";
    htmlInput += "<tr>";

    //Column Headers
    for (i = 0; i <= columns; i++){
        htmlInput += ("<td><b>" + i + "</b></td>");
    }
    htmlInput += "</tr>";
    for (i = 1; i <= rows; i++){
        htmlInput += ("</br><tr><td><b>" + i + "</b></td>");

        for (j = 1; j<= columns; j++){
            var multiplyResult = i * j;
            htmlInput += ("<td>" + multiplyResult + "</td>");
        }
        htmlInput += "</tr>";
    }
    htmlInput += "</table>";
    document.getElementById('tablePlacement').innerHTML = htmlInput;
    }

    };
    </script>
</head>
<body>
    <form id="input1Form">
        Rows: <input type="text" id="input1">
    </form>
    <form id="input2Form">
        Columns: <input type="text" id="input2">
    </form>
    <button type="button" onclick="doWork()">Enter</button>
    <div id="tablePlacement">
    </div>
</body>

1 Answer 1

1
 if(rows <=0 || rows >= 10){
    document.getElementById('tablePlacement').innerHTML = "Input rows must be between 1 and 9";
}

 if(cols <=0 || cols >= 10){
    document.getElementById('tablePlacement').innerHTML = "Input cols must be between 1 and 9";
}
Sign up to request clarification or add additional context in comments.

2 Comments

I know how to write the code. I'm more confused about where to actually place it. Would this be a nested if statement. How would it work?
You could just put all conditions into one if statement like this if (rows is not a number, or rows <= 0, or rows >= 10, or cols is not a number, or cols is <= 0, or cols is >= 10) { render generic error message like "Rows and Columns must be positive numbers less than 10" } else { ... render table ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.