Matrix with Java Script

0

I need help with a job, I am new in what is programming and the teacher that I play does not teach us well what to say. I explain roughly what is the exercise: From a budget entered by keyboard I have to distribute among 37 municipalities, in turn each has to distribute to two more areas. And so for each municipality. The problem is that I have to use a matrix and I do not know how to apply it. I clarify that each municipality has a different percentage.

    
asked by BrianAlex 02.11.2017 в 03:08
source

1 answer

1

Do the example you are saying, although if it is a task, you should do it yourself. The only thing that is necessary is to make the correct distribution of the initial percentages, the rest works well.

Html

<h1>Porcentaje
  <input id="numero"/>
  <input type="button" value="Enviar" onclick="calcularPorcentaje()"/>
</h1>
<div id="salida"></div>

javascript

  var cantidadMunicipios=37;
  var municipios=[];
  for (var i=0;i<cantidadMunicipios;i++){
    // Creando número aleatorios.
    // este valor debe ajustarse todos los municipios para que el total de los 37 sea 100%, porque de otro modo se pasaría del presupuesto.
    var porcentaje=Math.round(Math.random()*30)+1;  
    var barrios=[Math.round(Math.random()*49)+1,Math.round(Math.random()*49)+1];
    //incrusta un array dentro de otro, así crea un array bidimensional
    // lo mismo sucede con la variable Barrios, ese sería el tercer nivel del array
    municipios.push(["Municipio "+(i+1),porcentaje,barrios]);
  }

var calcularPorcentaje=function(){
  var num=document.getElementById('numero').value;
  document.getElementById('salida').innerHTML = "";
  for (var i=0;i<cantidadMunicipios;i++){
    var tempElement = document.createElement('div');
        var newDiv = document.createElement("div");         
        var p1 = document.createElement('p');
        var valorMunicipio=(num*municipios[i][1])/100;
        p1.innerHTML = "<b>"+municipios[i][0]+":</b> %"+municipios[i][1]+" => "+valorMunicipio;
        document.getElementById('salida').appendChild(p1);  
        for (var j=0;j<municipios[i][2].length;j++){                
            var p = document.createElement('div');                          
            p.innerHTML = "<div style='margin-left:25px'> Barrio"+(j+1)+": %"+municipios[i][2][j]+" => "+(valorMunicipio*municipios[i][2][j])/100+"</div>"
            document.getElementById('salida').appendChild(p);   
        }           
    }   
}

functional example

    
answered by 02.11.2017 / 05:25
source