Home > Mobile >  Struggling with an element in my Javascript code
Struggling with an element in my Javascript code

Time:10-25

I am to make a script that will ask the user to enter numbers until they type quit. I have this working, but I am struggling with the final element where I have to calculate the number of entries (not sum) the user submitted not including the var - quit.

Any assistance would be greatly appreciated.

<script>
 

var userWord = "quit";
var numList = Array();
var total = 0;

do {

userWord = prompt ("Enter a number of the word quit");
userWord = userWord.toLowerCase();
numList.push(userWord);
}
while (userWord != "quit")

document.writeln("<p>");
for (t = 0; t < numList.length - 1; t  ) {
document.write(numList[t]);
if (t < numList.length - 2) {
document.writeln(", ");
}

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

</script>
 

CodePudding user response:

You can use numList.length-1 to count how many numbers were stored in the numList array, here is a working example:

 

var userWord = "quit";
var numList = Array();
var total = 0;

do {

userWord = prompt ("Enter a number or the word quit");
userWord = userWord.toLowerCase();
numList.push(userWord);
}
while (userWord != "quit")

document.writeln("<p>");
for (t = 0; t < numList.length - 1; t  ) {
document.write(numList[t]);
if (t < numList.length - 2) {
document.writeln(", ");
}

}
document.writeln(`<br>You typed ${numList.length-1} number(s)`);
document.writeln("</p>");
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

https://dotnetfiddle.net/jKbPOZ

Here is the view with javascript:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index4</title>
    <script>
        var userWord = "quit";
        var numList = Array();
        var total = 0;

        do {

            userWord = prompt("Enter a number of the word quit");
            userWord = userWord.toLowerCase();
            numList.push(userWord);
        }
        while (userWord != "quit")

        document.writeln("<p>");
        for (t = 0; t < numList.length - 1; t  ) {
            document.write(numList[t]);
            if (t < numList.length - 2) {
                document.writeln(", ");
            }

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

        document.writeln("<p>");
        document.writeln("<table>");
        document.writeln("<tr>");
        document.writeln("<td>");
        document.writeln("Total # of items:");
        document.writeln("</td>");
        document.writeln("<td>");
        document.writeln(numList.length - 1);  /*subtract 1 for the word quit*/
        document.writeln("</td>");
        document.writeln("</tr>");
        document.writeln("</table>");
        document.writeln("</p>");
    </script>
</head>
<body>
    <div>
    </div>
</body>
</html>

CodePudding user response:

There is an easier way. You can just use Array.prototype.join() to convert the array to a string with a delimiter of your choice.

Below is an example of this. Also note that document.write isn't commonly used. Instead we can just use a div element and write into it using the innerHTML property.

Final thing is the loop. Its up to you but I usually just loop forever and break when I receive the quit input. It can be cleaner but that's personal choice. Bear in mind that in your code you push the "quit" string into the result array, probably not what you want.

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

        const div = document.querySelector('div');
        const numList = [];
        
        while (true) {
        
            const userWord = prompt ("Enter a number of the word quit");
            if(userWord == null || userWord.toLowerCase() == 'quit') {
                break;
            }
            numList.push(parseInt(userWord, 10));
        }
        
        div.innerHTML = "<p>You typed "   numList.length   
                        " numbers: "    numList.join(', ')   "</p>";
    
    </script>
</html>

Checking that non integer input values are handled well by the code is an exercise left for the reader.

  • Related