Struggling to create a table from prompts

So I’m trying to create a simple table that would do the following:

Ask the user for their gallons used, as well as miles driven, then divide miles driven by gallons used to show their miles per gallon. I’m just not quite sure how to get the answers from the prompt into my table, I’ve tried using document.writeIn to create everything but couldn’t figure out how to get that to work (if that’s something that could work). I don’t expect anyone to do the code for me, just need a nudge in the right direction!

So far I have this as my HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mileage Calculator</title>
    <link rel="stylesheet" href="7.11.css">
</head>
<body>
    <table>
        <tr>
            <th>Gallons Used</th>
            <th>Miles Driven</th>
            <th>Miles Per Gallon</th>
        </tr>
        <tr>
          <td></td>
        </tr>
    </table>
    <script src="7.11.js"></script>
</body>
</html>

and this as my JavaScript:


let gallon = prompt("Enter gallons used:", "0");
let milesDriven = prompt("Enter miles driven:", "0");
parseInt(gallon);
parseInt(milesDriven);

let mpg = milesDriven / gallon
    

The CSS probably isn’t important but I figure I’ll put it here just in case:

table,
th,
td {
  border: 1px solid black;
}

table {
  border-collapse: collapse;
}
th,
td {
  padding: 15px;
  text-align: left;
}

You’ll need to use the browser’s HTML DOM methods to update the content of the element you want to put the mpg value in. Here’s something I found in a quick google search that seems to be a good introduction:

It will probably be helpful if you give the element you are going to update an id so that you can refer to it directly using document.getElementByID().

1 Like

I finally got it to work! I still don’t understand everything, but I figured the basics of assigning an id and using the .js to re-assign with .innerHTML.

JS:


let gallon = prompt("Enter gallons used:", "0");
let milesDriven = prompt("Enter miles driven:", "0");
parseInt(gallon);
parseInt(milesDriven);
let mpg = milesDriven / gallon 



const gallons = document.getElementById("gallons-used")
gallons.innerHTML = gallon

const mileys = document.getElementById("miles-driven")
mileys.innerHTML = milesDriven

const milesPerGallon = document.getElementById("mpg")
milesPerGallon.innerHTML = mpg

HTML:

body>

    <section class="container">

        <table id="my-table"></table>

        <table>
            <tr>
                <th>Gallons Used</th>
                <th>Miles Driven</th>
                <th>Miles Per Gallon</th>
            </tr>

            <tr>
                <th id="gallons-used"></th>
                <th id="miles-driven"></th>
                <th id="mpg"></th>
            </tr>
            
        </table>
    </section>  
    <script src="7.11.js"></script>
  
</body>
</html>



If you see anything I could make better than what I used let me know, and THANK YOU!!

I’d use .textContent instead of .innerHTML, because you’re reading a user input (and you never know what a user writes into an input field):

gallons.textContent = gallon

To give you an idea what could happen if you use innerHTML, enter this into your input field (copy/paste the whole line) and see what happens:
<script>alert('BOO')</script>

1 Like

So I have a simple miles per gallon table I’m trying to loop through via prompts. The goal is to have the code ask for miles, gallons, output into a table, and repeat until the user types a value below zero. So they can have as many rows/tries at calculating as they want ,until they put a negative value then they can look at it. I know this may not be the best practical use of a website, but I’m just trying to use this an an exercise to modularly figure out things.

I can’t quite get it to loop though, what am I doing wrong? Wrong type of loop? Wrong idea?
Here’s the JavaScript:

do {
    let milesDriven = prompt("Enter miles driven:", "0");
    let gallon = prompt("Enter gallons used:", "0");
        parseInt(milesDriven);
        parseInt(gallon);
    let mpg = milesDriven / gallon; 
    var table = document.getElementById("table-body");
    var row = table.insertRow(0);
    var cellOne = row.insertCell(0);
    var cellTwo = row.insertCell(1);
    var cellThree = row.insertCell(2);
        cellOne.innerHTML = gallon;
        cellTwo.innerHTML = milesDriven;
        cellThree.innerHTML = mpg;
    } 
    while(milesDriven > 0);

and here’s the HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mileage Calculator</title>
    <link rel="stylesheet" href="7.11.css">
</head>
<body>

    <section class="container">

        <table id="myTable">
            <thead>
                <tr>
                    <th>Gallons Used</th>
                    <th>Miles Driven</th>
                    <th>Miles Per Gallon</th>
                </tr>
            </thead>
            <tbody id="table-body">
                
             </tbody>
        </table>
    </section>  
    <script src="7.11.js"></script>
  
</body>
</html>

declare using var,
Currently, we can’t access it outside the body of loop;

1 Like

declare using var ,
Currently, we can’t access it outside the body of loop;

Do you mean just initialize the variables outside of the loop, or change the let to a var?

EDIT: Ah, I see what you mean, I’m not sure why it didn’t work but using all var vs having both let and var in the loop. Curious as to why but it works now!

You shouldn’t declare the variables in the loop;
Just declare it before the loop, and then use them inside the loop…

As far as this example is considered,
variables declared with let have block scope, they can’t be used outside the block on which they’re defined. and therefore you were receiving error: milesDriven is not defined in this scope.

Hope it’s clear.
Here’s more info:

So with the help of the forum I’ve figured out how to loop a mileage calculator table with prompts, the issue I’m having is that it works, but maybe it’s not the optimal way of doing it, as currently I can have a break in my while loop, but am unsure what the condition should be.

while () {
    var milesDriven = prompt("Enter miles driven:", "0");
    if (milesDriven < 0) {
        break;
    }
    var gallon = prompt("Enter gallons used:", "0");
        parseInt(milesDriven);
        parseInt(gallon);
    var mpg = milesDriven / gallon; 
    var table = document.getElementById("table-body");
    var row = table.insertRow(0);
    var cellOne = row.insertCell(0);
    var cellTwo = row.insertCell(1);
    var cellThree = row.insertCell(2);
        cellOne.innerHTML = gallon;
        cellTwo.innerHTML = milesDriven;
        cellThree.innerHTML = mpg;
}

Essentially I just want the prompts to loop forever until the user types a value below or equal to 0. I thought maybe making the condition if it’s an integer it wont run again, but essentially I’m asking is there a way to use a while loop, but if one of the conditions is false have it not run at all? If so, what’s the use of the conditions if they’re doing the same thing as the break;

As a side question, is there a way to have the table load in real-time versus a blank page, which only loads after the prompts are done? At least through the prompt method.

A while loop continues to loop while the condition in the parens is true. I’m guessing you can think of something to put in there that will always be true. <-- hint

1 Like

Though, I always recommend a way for the user to stop a loop for user input that does not involve crashing the program. Like a boolean doAgain variable.

Ah, so it will run the loop at least once before checking on the condition? So how about this, prompting the user first, then using an if statement to see if the values are greater than 0, and then running the loop after that.

var milesDriven = prompt("Enter miles driven:", "0");
var gallon = prompt("Enter gallons used:", "0");
    parseInt(milesDriven);
    parseInt(gallon);
var mpg = milesDriven / gallon; 

if (milesDriven > 0  || gallon > 0) {
    var table = document.getElementById("table-body");
    var row = table.insertRow(0);
    var cellOne = row.insertCell(0);
    var cellTwo = row.insertCell(1);
    var cellThree = row.insertCell(2);
        cellOne.innerHTML = gallon;
        cellTwo.innerHTML = milesDriven;
        cellThree.innerHTML = mpg;
} else {
    break;
}


while (milesDriven > 0 || gallon > 0) {
    var table = document.getElementById("table-body");
    var row = table.insertRow(0);
    var cellOne = row.insertCell(0);
    var cellTwo = row.insertCell(1);
    var cellThree = row.insertCell(2);
        cellOne.innerHTML = gallon;
        cellTwo.innerHTML = milesDriven;
        cellThree.innerHTML = mpg;
    var milesDriven = prompt("Enter miles driven:", "0");
    var gallon = prompt("Enter gallons used:", "0");
        parseInt(milesDriven);
        parseInt(gallon);
    var mpg = milesDriven / gallon; 
}

Currently this won’t run, but I think I’m on the right track?

if you have already a topic for this project, please keep using it - I can merge the posts in this topic to that one

You’re repeating way too much code here. I’m not sure what your second version has to gain over your first version? In fact, I’m not sure what you are exactly trying to do. I think you need to spell it out for us.

Ah I’m sorry, I’m still new here, I’ll keep it to one thread for future posts!

If you tell me which threads are for this project, I can merge them to one thread

So I’d like it to prompt the user, but if the value is lower or equal to 0 to not create a table and stop running. The issue I had with the while loop is that it would still print out the negative values or a 0 into the table. I can’t figure out how to break out of the loop before calculating without an additional check. So by repeating the code I was hoping to run it once before entering the loop, that way I could hopefully set my while loop condition to check the variable values before running.

and
https://forum.freecodecamp.org/t/do-while-loop-not-executing/429776/3

Yeah, don’t repeat yourself. Set the while loop condition to run at least once.

The first thing you did in the while loop was prompt the user for miles and then the very next thing you did was check what they entered to see whether you needed to break out of the loop. That is exactly what you said you wanted to do. So theoretically it should have worked. I did noticed that you had the check as:

if (milesDriven < 0) {

Which means that entering 0 wouldn’t have stopped the loop. Also, the paranoid programmer might convert what the user entered to a Number and then check to make sure it is actually a Number.