call a function inside another function in js

2

Hello, to not have to repeat the same 2x code and change only the end I wanted to do 3 different functions, the vectors () and the two that will call vectors () and then execute their code, but I'm not What am I doing wrong?

var i;
function vetores() {
    var n = new Array(10);
    for(i = 0; i < 10; i++) {
        var input = "input";
        var inputNumero = "" + i.toString();
        var inputId = input.concat(inputNumero);
        n[i] = document.getElementById(inputId).value;
    }
}
function calcularMaior() {
    function vetores();
    var max = Math.max(...n);
    alert("Maior valor: " + max);
}
function calcularMenor() {
    function vetores();
    var min = Math.min(...n);
    alert("Menor valor: " + min);
}
    
asked by anonymous 24.05.2018 / 21:11

1 answer

2

I do not quite understand the code, it's a bit confusing and the HTML was not passed. I tried adjusting the code to remove the errors the way I understood it, see if it helps:

function vetores() {    
    var i;
    var n = new Array(10);
    for(i = 0; i < 10; i++) {
        var input = "input";
        var inputNumero = "" + i.toString();
        var inputId = input.concat(inputNumero);
        n[i] = document.getElementById(inputId).value;
    }
    return n;
}
function calcularMaior() {
    var n = vetores();
    var max = Math.max(...n);
    alert("Maior valor: " + max);
}
function calcularMenor() {
    var n = vetores();
    var min = Math.min(...n);
    alert("Menor valor: " + min);
}
    
24.05.2018 / 21:27