Am I using the do/while correctly?

Hi,

I am trying to create this in javascript, but am stuck with the loop, or maybe it should be a if/then? I can’t figure out where to place the true/false either.

let boxes = {
    a1: 2, 
    b2: 1,
    c3: 4, 
    d4: 4, 
    e5: 6, 
    f6: 5, 
    g7: 2, 
    h8: 12, 
    i9: 5, 
    j10: 19, 
    k11: 1, 
    l12: 0,
};
do {

let sum1 = (boxes.b2 + boxes.l12);
boxes.d4 = sum1;

let sum2 = (boxes.g7 * boxes.g7);
boxes.g7 = sum2;

let sum4 = (boxes.d4 + boxes.a1);
boxes.d4 = sum4;

let sum5 = (boxes.g7 - boxes.b2);
boxes.g7 = sum5
}

while (boxes.d4 !== boxes.i9);
//sum3 TRUE

let sum6 = (boxes.c3 + boxes.i9);
boxes.j10 = sum6; 

let sum7 = (boxes.g7 - boxes.j10);
boxes.k11 = sum7


if (boxes.k11 == '55')
console.log('Correct')

// console.log(boxes)

Thanks

Hello there,

You are quite close to getting it, from what I can see. Just step 1 should not be within the loop:

Hope this helps

Ah, thanks @Sky020 . I changed that by mistake when i copied it to this forum.

However, it still doesn’t return correct. Am I supposed to use the do/while like this?

Actually, after reading it again, I realise do...while is not doing what you need.

You need the loop only to run, if (num in box 4 === num in box 9) - sometimes, the loop must not run.

Hope this helps

How do I get it to loop back to sum 2 until true?

A simple while loop should do it.

I thought I would share the solution a friend sent me for beginners, like me, who might want to know the answer.

This is using my original code.

let boxes = {
    a1: 2, 
    b2: 1,
    c3: 4, 
    d4: 4, 
    e5: 6, 
    f6: 5, 
    g7: 2, 
    h8: 12, 
    i9: 5, 
    j10: 19, 
    k11: 1, 
    l12: 0,
};

// sum 1
let sum1 = (boxes.b2 + boxes.l12);
boxes.d4 = sum1;

// declare false
var ok=false;

do{
    // sum 2
    boxes.g7 = (boxes.g7 * boxes.g7);

    // if true
    if (boxes.d4===boxes.i9)
    {
        ok=true;
    }

    //if false
        else{

    // sum 4
        let sum4 = (boxes.d4 + boxes.a1);
        boxes.d4 = sum4;
    
        // sum 5
        let sum5 = (boxes.g7 - boxes.b2);
        boxes.g7 = sum5
    }
}while (!ok);

// sum 6
boxes.j10 = (boxes.c3 + boxes.i9);

// sum 7
boxes.k11 = (boxes.g7 - boxes.j10);

console.log(boxes.k11);

This is using improved code.

let arr=[2,1,4,4,6,5,2,12,5,19,1,0];

arr[3] = (arr[1] + arr[11]);

var ok=false;

do{
    arr[6] = (arr[6] * arr[6]);

    if (arr[3]===arr[8])
    {
        ok=true;
    }
    else{
        arr[3] = (arr[3] + arr[0]);
        arr[6] = (arr[6] - arr[1]);
    }
}while (!ok);

arr[9] = (arr[2] + arr[8]);

arr[10] = (arr[6] - arr[9]);

console.log(arr[10]);

Another way to write the loop. Again more close to what you would see in real life.

    arr[6] *= arr[6];

    if (arr[3]===arr[8])
    {
        ok=true;
    }
    else{
        arr[3] += arr[0];
        arr[6] -= arr[1];
    }
}
arr[6]=arr[6] is the same as arr[6]=arr[6]arr[6]

I hope that helps someone.

one issue here was that that is all your loop, as you put there a semicolon and that ends a statement, and you need to surround the loop body with {}


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Sorry, I thought I had put the code using the </> button.

I’m still not sure where that loop should be using the { }.

The semicolon bit makes sense.

Is my OP code close to being correct then?

the only way to find out is to try, surround what you want in the loop with {} and see if it works
put the { in place of the ; and the } where you want the loop to end

this is not easy to do with a simple loop because of where the check is based on one of the things that loop,
maybe a for loop not used that appropriately… but it reflect more the block diagram, even if it doesn’t use a do…while

box4 = box2 + box12
for (box7 *=  box7; box4 !== box9; box7 *= box7) {
   box4 += box1
   box7 -= box2
   box10 = box3 + box9
}
box11 = box7 - box10
console.log(box11)

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