how to last values of a variable in several different functions, in javascript,

Asked

Viewed 57 times

-4

function CalcularSoma(valor1, valor2, soma) {
    var valor1 = document.getElementById("formValor1").value;
    var valor2 = document.getElementById("formValor2").value;
    var soma;
    soma = parseInt(valor1) + parseInt(valor2);
    document.getElementById("formResultado").value = soma;
}
function CalcularMult(valor1, valor2, mult) {
    var valor1 = document.getElementById("formValor1").value;
    var valor2 = document.getElementById("formValor2").value;
    var mult = (valor1 * valor2);
    document.getElementById("formResultado").value = mult;
}
function CalcularDiv(valor1, valor2, div) {
    var valor1 = document.getElementById("formValor1").value;
    var valor2 = document.getElementById("formValor2").value;
    var div = (valor1 / valor2);
    document.getElementById("formResultado").value = div;
}
function CalcularSub(valor1, valor2, sub) {
    var valor1 = document.getElementById("formValor1").value;
    var valor2 = document.getElementById("formValor2").value;
    var sub = (valor1 - valor2);
    document.getElementById("formResultado").value = sub;
}```
  • Simply declare the variable outside the function.

  • @Jeanextreme002 The problem of declaring out of function is happening that (and you don’t know if it’s going to happen because the question doesn’t give more details)

  • 2

    Can you explain the question better? I’m not sure what you’re trying to solve.

  • How are you calling these functions, Marcelo?

  • by ultilizing the onclick event, however while trying to really avoid the repetition of the code by creating the variable in all functions the code of the error, I would like to understand better how to get remove them and stay a more visible code simply that, I’m starting now I have little knowledge in the language itself.

  • @I wonder if you have a way to declare these variables out of function and power in other functions, not to be repeating in all functions the same variables value1 and value2.

Show 1 more comment

2 answers

0

It would be better to structure the code in a different way. Note that the lines:

var valor1 = document.getElementById("formValor1").value; var valor2 = document.getElementById("formValor2").value;

They are being repeated in all functions. It would be simpler to separate these lines out of the functions. Your function must have only one purpose, for example, to add up the numbers. In your case you are using the function to receive the input and process the data, try to separate those responsibilities better when you build the function, so you will not repeat code and have huge unnecessary codes.

0


I would change the functions to directly receive the value, in addition to changing a lot of other things in your code:

function calcularSoma(valor1, valor2) {
    let soma = valor1 + valor2;
    return soma;
}
function calcularMult(valor1, valor2) {
    let mult = (valor1 * valor2);
    return mult;
}
function calcularDiv(valor1, valor2) {
    let div = (valor1 / valor2);
    return div;
}
function calcularSub(valor1, valor2, sub) {
    let sub = (valor1 - valor2);
    return sub;
}

Then you would use these functions like this:

let valor1 = document.getElementById("formValor1").value;
let valor2 = document.getElementById("formValor2").value;

document.getElementById("formResultado1").value = calcularSoma(valor1, valor2);
document.getElementById("formResultado2").value = calcularMult(valor1, valor2);
document.getElementById("formResultado3").value = calcularDiv(valor1, valor2);
document.getElementById("formResultado4").value = calcularSub(valor1, valor2, sub);

This is all because using global variables generates unexpected loading and processing errors, which I believe is not your intention, and creating a function that accesses an element of the DOM, reads its value, executes something and then modifies another element of the DOM is not, exactly, easy to read. If you, for example, were to insert this function in an event onclick, eute would indicate creating a function to perform this change, such as:

function exibeSoma(){
    let valor1 = document.getElementById("formValor1").value;
    let valor2 = document.getElementById("formValor2").value;
    document.getElementById("formResultado").value = calcularSoma(valor1, valor2);
}

See how it is more readable, in addition to facilitating maintenance?

  • I wanted to know why the answer was negative :/

  • 1

    I don’t know either, I thought it was good!

  • 1

    really much more legible, thank you so much for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.