Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

Problem with variables and functions in JavaScript

I have a little problem with JS. I want to make a simple script that calculate the results of a second grade equation like X^2+X+Q. The problem is that I don't know how to manipulate variables, so I'm not getting the result I want. Here's the code.

var a, b, c;

function input_a(a) {
    a = prompt("Enter your a");
}

function input_b(b) {
    b = prompt("Enter your b");
}

function input_c(c) {
    c = prompt("Enter your c");
}

function calculate(a, b, c) {
    var delta = (b*b) - (4*a*c);
    if (delta < 0) {
        document.getElementById("result").innerHTML = "Impossible, delta is < 0";
    } else if (delta > 0) {
        document.getElementById("result").innerHTML = delta;
    } else {
        document.getElementById("result").innerHTML = "ERROR";
    }
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.header {
    background-color: rgb(70, 69, 69);
    width: 100%;
    display: inline-block;
    text-align: center;
    padding: 30px 0px;
}

.title {
    color: rgb(255, 255, 255);
}

.example {
    color: white;
}

.button_wrap {
    text-align: center;
    background-color: rgb(230, 174, 34);
    width: auto;
}

.button {
    display: inline-block;
    background-color: rgb(70, 69, 69);
    width: auto;
    height: auto;
    padding: 20px;
    margin: 10px 30px;
}

.start {
    display: block;
    background-color: rgb(70, 69, 69);
    margin: 15px auto 0px auto;
    padding: 20px;
}

.result_wrapper {
    text-align: center;
    padding-top: 100px;
    background-color: rgb(187, 141, 25);
    width: auto;
}

#result {
    display: inline-block;
    width: auto;
    height: auto;
    background-color: rgb(70, 69, 69);;
    padding: 20px;
    margin: 10px;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Equazioni</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
</head>

<body>
    <header class="header">
        <h1 class="title">EQUATIONS</h1> <br>
        <h2 class="example">X^2  +-  X  +-  Q</h2>
    </header>

    <div class="button_wrap">
        <button onclick="input_a()" class="button">INSERT YOUR A</button>
        <button onclick="input_b()" class="button">INSERT YOUR B</button>
        <button onclick="input_c()" class="button">INSERT YOUR C</button>
        <button onclick="calculate()" class="start">CALCULATE</button>
    </div>

    <div class="result_wrapper">
        <p id="result"></p>
    </div>

</body>

</html>
question from:https://stackoverflow.com/questions/65846472/problem-with-variables-and-functions-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The script actually prints ERROR, so delta must be equal to 0.

That conclusion isn't true at all. It could also be null, or undefined, or in this case... NaN. Don't assume, validate:

console.log(delta);

The reason is because you re-declare local variables and never define them.

This:

function input_a(a)

Should be this:

function input_a()

Repeat for all of the other functions. This is because having those parameters means that any time you reference that variable within the function you're referencing the one that was passed to it, not the global one. And you don't pass anything to the functions, but are instead trying to use global variables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...