Prompt asking for 10 math problems that don't equal 69 or 42 is tricky and unsure how to reduce into just two nested loops

Viewed 20

Prompt: Using nested loops output 10 math problems x+y that do not equal 69 or 42 for students using nested loops.

My attempt, any suggesting's would be helpful. It does works below but without nested loops.

for(var i =0; i < 10;i++)
    {
    var total;
    var x; 
    var y;

    while(total != 69 && total != 42)
       {
    while(x != 69 && x != 42)
    {
       x = Math.floor(Math.random() * 100);
        if(x != 69 && x != 4)
        {
            break;
        }
    }
    while(y != 69 && y != 42)
    {
       y = Math.floor(Math.random() * 100);
        if(y != 69 && y != 42)
        {
            break;
        }
    }
        if (total != 69 && total != 42)
        {
            break;
        }
    }


console.log(x + "+" +y);
                  };
1 Answers

the first loop you have is correct, you are going to iterate 10 times to create 10 math problems. then all you need is an inner loop to keep changing the values of x and y if they = 69 or 42 or the sum of x + y = 69 or 42. Below you can see a while loop nested inside a for loop, thus having a nested loops:

for(let i = 0; i < 10; i++){
  let x = Math.floor(Math.random() * 100);
  let y = Math.floor(Math.random() * 100);

  while(x == 69 || x == 42 || y == 69 || y == 42 || x + y == 69 || x + y == 42){
    x = Math.floor(Math.random() * 100);
    y = Math.floor(Math.random() * 100)
  }
  console.log((i + 1) + ') ' + x + ' + ' + y)
}
Related