Fill vectors with data and perform calculations in javascript

0

I have the following exercise:

** The 3 coordinates of 3 vectors in space are read (v1, v2, v3). Make an app in javascript that shows the following:

The magnitude of each vector

The product bridge from v2 to V1

The sum of v1 + v2

The difference of v3-v1

The distance between v2 and v1 **

This I have to do in javascript and I tried the following:

<script>
var valuesVector = [];

function Vector(i,j,k){
    this.i = i;
    this.j = j;
    this.k = k;
}

function obtenerValores(){
    var i = document.getElementById('i-1').value;
    var j = document.getElementById('j-1').value;
    var k = document.getElementById('k-1').value;

    return i;
    return j;
    return k;
}
function CreaVector(){
    var vector = new Vector(obtenerValores());
    valuesVector.push(vector);
    mostrarCalculo();
} 
function mostrarCalculo(){
    var result = "";
    for(var i=0; i<valuesVector.lenght; i++){
        result+= 'i:' +  valuesVector[i].i + 
                'j:'+ valuesVector[i].j + 
                'k:'+ valuesVector[i].k + '\n';
    }
    document.getElementById('resul').innerText = result;
}
</script>

    <h4>VECTOR V1</h4>
    <form action="" class="form-horizontal">
    <input class="form-control" type="text" id="i-1">
    <input class="form-control" type="text" id="j-1">
    <input class="form-control" type="text" id="k-1">
    <button class="btn btn-primary" onclick="obtenerValores(); 
    CreaVector();" type="button">Calcular</button>
    </form>
    <h4 id="resul"></h4>

This is what I have tried without obtaining any results, for now I want to show the data that I have entered in the input, but it does not show me anything. And I would also like to know that since there are 3 vectors V1 V2 and V3 and each one has to have 3 data inside it, I would have to do a different function to obtain and calculate the data for each of the vectors? I need help with this, I do not know much about javascript and I'm very lost.

    
asked by Alejo Mendoza 19.02.2018 в 03:13
source

1 answer

0

the code of the function is incorrect

    function obtenerValores(){
         var i = document.getElementById('i-1').value;
         var j = document.getElementById('j-1').value;
         var k = document.getElementById('k-1').value;

        return i;
        return j;
        return k;

}

When you do that you are only returning the value of 'i-1' since the first return cuts the others. You should return at this point a vector with the three values and start working from there. From the html you can directly call CreateVector, since from there you are also using get values.

This code works ok (just add the values of the 3 input as an example to guide you), leave the Vector function, but I do not use it.

          <html>

          <script>

             var valuesVector = [];

             function Vector(i,j,k){
                      this.i = i;
                      this.j = j;
                      this.k = k;
              }

             function obtenerValores(){
                   var i = document.getElementById('i-1').value;
                   var j = document.getElementById('j-1').value;
                   var k = document.getElementById('k-1').value;

                 return [i,j,k];
                    }
              function CreaVector(){

                    valuesVector = obtenerValores(); 
                    var resultado=0;
                    for(var index=0;index<valuesVector.length;index++)
                     {

                         resultado=resultado+Number(valuesVector[index]);

                     }
                    document.getElementById('resul').innerText = resultado;

                  } 


         </script>


                  <body>
                    <h4>VECTOR V1</h4>                          
                      <input class="form-control" type="text" id="i-1">
                      <input class="form-control" type="text" id="j-1">
                      <input class="form-control" type="text" id="k-1">
                      <button class="btn btn-primary" 
                      onclick="CreaVector();">Calcular</button>   

                   <h4 id="resul"></h4>
                   </body>

                   </html>

In your example showdown does not work because you put lenght when it is length

      function mostrarCalculo(){
               var result = "";
              for(var i=0; i<valuesVector.lenght; i++){
                     result+= 'i:' +  valuesVector[i].i + 
                     'j:'+ valuesVector[i].j + 
                     'k:'+ valuesVector[i].k + '\n';
                     }
                 document.getElementById('resul').innerText = result;
                }

I hope it's of your use.

Greetings.

    
answered by 19.02.2018 в 03:29