Enter formula with a for [closed]

0

I need to perform the following operation. Example, I have an array cantidad = [420, 940, 920, 850,970]; and% ancho = 4500 .

The formula works like this:

420 + 940 / 2 = 890;
940 / 2 + 920 / 2 = 930;
920 /2 + 850 /2 = 885;
850 / 2 + 970 /2 = 910;
4500-890-930-885-910 = 885;
4500-890-930-885-910 = 885 -970/2 = 400;

How do I do it in JavaScript?

    
asked by Eduard Zora 03.03.2017 в 19:55
source

2 answers

2

You could do the following:

var ancho = 4500;
var matriz = [420, 940, 920, 850, 970];
var num = 0;
var restoAncho = 0;
for(var i=0; i < matriz.length; i++){
    if(i === 0){
        num = matriz[i];
    }else{
        if(i > 1) num = matriz[i-1]/2;
        num = num+(matriz[i]/2);
        restoAncho -= num;
    }
}

var resultado = ancho + restoAncho -(matriz[matriz.length-1]/2) ;
console.log('resultado = '+ resultado);

I hope it serves you!

    
answered by 03.03.2017 / 20:34
source
1

Greetings Eduard,

You could start with a bit of reading, example link as you do a tour of an arrangement with JQuery.

After that, follow the logic you just wrote. I hope this serves you more, than writing you the code.

Good luck!

    
answered by 03.03.2017 в 20:27