# Javascript Loop Formula

Hi,

Over a week I can’t get the answer solve this scenario.

``````var a = 26
var b = [0.8,1.6,0.8]
``````
• 12 is constant

Step 1: 12 - 0.8 (First array) = 11.2
Step 2: a - 11.2 (Step 1 answer) = 14.8
Step 3: check, if step 2 answer is greater than 12 then do step 4 else do step 7
Step 4: 12 - 1.6 (Second array) = 10.4
Step 5 : a - (14.8 (S1 answer) + 10.4 (S4 answer) ) = 4.4
Step 6: check again if step 5 answer is greater than 12 then do step 4 but take (Third array), else do step 7.

Step 7: 4.4 (Final Answer) + 0.8 (Last array)

Need your help to turn into javascript formula.

Many thanks ahead!

What JavaScript code have you come up with so far? If we can see what that is, then we can help you understand why it’s not working.

``````var a = 26;
var b = [0.8,1.6,0.8];
var c = 12;
var final = 0
for (var i = 0; i < b.length; i++){
var d = c - b[i];
var e = a-d

if (e > 12){
// I don't know how to go to step 4
}
else{
final = e+b[b.length]
}

}
``````

Im stuck, please help…

At the moment, I’m having a bit of difficulty following the step sequence in the original post - is that exactly how the problem was described?

Well this would be an almost literal implementation of the algorithm you described… you might be missing a few cases, though (e.g. what happens if `step2 <= 12` and thus `step5` is `undefined`)…

``````// 12 is constant
const constant = 12

var a = 26
var b = [0.8, 1.6, 0.8]
var index = 0
var step1
var step2
var step4
var step5
var final

// Step 1: 12 - 0.8 (First array) = 11.2
step1 = constant - b[index++]

// Step 2: a - 11.2 (Step 1 answer) = 14.8
step2 = a - step1

// Step 3: check, if step 2 answer is greater than 12 then do step 4 else do step 7
if (step2 > constant) do {

// Step 4: 12 - 1.6 (Second array) = 10.4
step4 = constant - b[index++]

// Step 5 : a - (14.8 (S1 [not step 2?] answer) + 10.4 (S4 answer) ) = 4.4
step5 = a - step2 + step4

// Step 6: check again if step 5 answer is greater than 12 then do step 4 but take (Third array), else do step 7.
} while (step5 > constant && index < b.length)

// Step 7: 4.4 (Final Answer) + 0.8 (Last array)
final = step5 + b[b.length - 1]
console.log(final)
``````
1 Like

// Step 4: 12 - 1.6 (Second array) = 10.4
` step4 = constant - b[index++]`

why on the step4 it takes third array?

Hi there,

Finally I got the answer, thank you everyone!

``````// 12 is constant
var constant = 12

var a = 26
var b = [0.8,1.6,0.8]
var index = 0
var step1 = 0
var step2 = 0
var step4 = 0
var step5 = 0
var step6 = 0
var final = 0
var TotArr = [];

// Step 1: 12 - 0.8 (First array) = 11.2
step1 = constant - b[index++]

// Step 2: a - 11.2 (Step 1 answer) = 14.8
step2 = a - step1
TotArr.push(step1)
// Step 3: check, if step 2 answer is greater than 12 then do step 4 else do step 7
if (step2 > constant) do{

// Step 4: 12 - 1.6 (Second array) = 10.4
step4 = constant - b[index++]

// Step 5 : a - (14.8 (S1 [not step 2?] answer) + 10.4 (S4 answer) ) = 4.4
step5 = a - (step1 + step4)
TotArr.push(step4)

// Step 6: check again if step 5 answer is greater than 12 then do step 4 but take (Third array), else do step 7.
}while (step5 > constant && index < b.length){

TotArr = TotArr.reduce((a1, b1) => a1 + b1, 0);
step6 = a - TotArr

// Step 7: 4.4 (Final Answer) + 0.8 (Last array)
final = (step6) + (parseFloat(b[b.length - 1]))
console.log(final.toFixed(3))
}
``````

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.