Store input values in a javascript array

0

I am creating a matrix with dynamic input values. The program asks you for the order and the set of vectors. From that, it generates a bidimensional arrangement (with the values that you entered in the inputs, of course). For example, if I want to order 3 and 3 sets of vectors, the program creates the inputs so that in the end a 3x3 array is created. The problem is when I want an order greater than the number of vectors, say order 4 and number of vectors 3. The algorithm that I have in the else if (order == vectors) can not be applied to the case else if (order> vectors) . Any idea of being able to store an mxn array? Let's say I want to store this in an array from my inputs:

Any ideas? I'd really appreciate it, regards

HTML:

<select id="select_order">
            <option selected>Ingrese el orden del espacio vectorial</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
        </select>
    </div>

    <input id="number_vectors" type="text" class="number" placeholder="Ingrese el numero de vectores"><br>

    <button type="button" onclick="" id="vectors">Ingresar Vectores</button><br>

    <div class="" id="show_vectors">

    </div>

    <button type="button" onclick="" id="calcular">Calcular Independencia</button>

JS:

$("#calcular").hide();

var establecer = document.getElementById("vectors");

establecer.addEventListener('click', function(event){

    $("#calcular").show();

    var select_order = document.getElementById("select_order");
    var number_vectors = document.getElementById("number_vectors");

    let order = parseInt(select_order.value);
    let vectors = parseInt(number_vectors.value);


    var innerhtml = '';

    //GENERA INPUTS DINAMICOS

    for (var i = 0; i < vectors; i++) {
        for (var j = 0; j < order; j++) {
            innerhtml += "<input type='text' class='entrada' value='' placeholder='' id='" + (j + 1) + "' name='" + (j + 1) + "' size=5>"+"<br>";
        }
        innerhtml += "<br>";
    }

    var show_vectors = document.getElementById("show_vectors");

    var calcular = document.getElementById("calcular");

    show_vectors.innerHTML = innerhtml;

    calcular.addEventListener('click', function(event){

        if(vectors > order){

            alert("Son linealmente dependientes");

        } else if (order==vectors) {

            //arreglo principal
            let array = [];

            //selecciona los inputs creados
            let inputs = document.querySelectorAll('.entrada');

            //arreglo temporal
            let temp = [];

            for (var i = 0; i < inputs.length; i++) {

                //añade al arreglo temporal para el 2D
                temp.push(inputs[i].value);

                //si es igual al orden de la matriz
                // entonces lo añadimos al arreglo principal y
                // reseteamos el temporal
                if(temp.length == order){
                    array.push(temp);
                    temp= [];
                }
            }

            //imprimir matriz

            document.write("Matriz generada por los vectores: " + "<br>");

            for (var i = 0; i < array.length; i++) {
                for (var j = 0; j < array.length; j++) {
                    document.write("\t"+array[i][j]);
                }
                document.write("<br>");
            }


        } else if(order > vectors){

            //AQUI ES DONDE TENGO PROBLEMAS


        }else{
            alert("Son independientes");
        }





    });


});
    
asked by Julio Guzman 02.03.2018 в 18:57
source

0 answers