0

Well, first of all I'm using JUST javascript, so no html, it's only a js file that I'm runnning in console because I'm trying to practice. (I've also installed nodejs) The thing is, When I run the code, It just doesn't let me input anything, and ends up with nan values in each variable. I tried 2 variants but I just get almost the same result.

function ejercicio1_4() {

    let km;
    let pr
    let kml
    for(i=0; i<3; i++) {

        if(i==1) {
            console.log("Write your car km: ")
            process.stdin.on('data', function(data) {
                km = data;
                process.exit()
            })
        }else if (i==2) {
            console.log("Write the gas price: ")
            process.stdin.on('data', function(data) {
                pr = data;
                process.exit()
            })
        }else if (i==3) {
            console.log("Write how much km your car drives for each gas liter: ")
            process.stdin.on('data', function(data) {
                kml = data;
                process.exit()
            })
        }

    }
    let liters_consumed = (km/kml)
    let money_spent = pr/liters_consumed
    console.log("you've consumed: " + liters_consumed + "liters of gas. And you've spent: " + money_spent + "dollars in total.")

    // console.log("Write your car km: ")
    // process.stdin.on('kmm', function(kmm) {
    //     km = kmm.toNumber()
    //     process.exit()
    // })
    // console.log("Write the gas price: ")
    // process.stdin.on('prr', function(prr) {
    //     pr = prr.toNumber()
    //     process.exit()
    // })
    // console.log("Write how much km your car drives for each gas liter: ")
    // process.stdin.on('kmll', function(kmll) {
    //     kml = kmll.toNumber()
    //     process.exit()
    // })
    // let liters_consumed = (km/kml)
    // let money_spent = pr/liters_consumed
    // console.log("you've consumed: " + liters_consumed + "liters of gas. And you've spent: " + money_spent + "dollars in total.")
}
ejercicio1_4()

I post the output pictures: Output after using the first script Second script: Output after using the second script

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28

0 Answers0