Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
p5js one tough puzzle returning not enough numbers to console

i'm trying to create a one tough puzzle solver thing, can anyone help me on why this is only showing 4 numbers in console instead of 8.

s1 = [1,-4,-1,2];
s2 = [2,-4,-3,4];
s3 = [4,-1,-3,3];
s4 = [3,-3,-4,4];
s5 = [2,-4,-3,2];
s6 = [4,-2,-4,3];
s7 = [1,-2,-4,2];
s8 = [1,-1,-4,4];
s9 = [1,-3,-3,4];
pieces = [s1,s2,s3,s4,s5,s6,s7,s8,s9];
correct =[];
pog = 0;

function setup() {
  createCanvas(400, 400);
  for(i=0;i<8;i++){
    for (h=1;h<8;h++){
      if(h==i){
        i++;
      }
      for(j=0;j<4;j++){
        if(pieces.at(i).at(1) == -pieces.at(h).at(3)){
          console.log(h);
          break
          i=h;
        } else {
          shiftL(pieces.at(h));
        }
      }
    }
  }
}

//BTW the piece above another piece is 3 pieces away!!!!

function draw() {
  background(220);
  frameRate(1);
}

function shiftL(array) {
  let sV = array.at(0);
  array.splice(0,1);
  array.splice(array.length-1,0,sV);
}

i'm trying to output more but it only shows 1,3,7,2. i'm adding more text so that this is able to be published.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is pretty hard to tell what you are trying to do and your for loop logic is very convoluted, here it is with comments and log statements:

  for (i = 0; i < 8; i++) {
    console.log(`begin i = ${i}`);
    for (h = 1; h < 8; h++) {
      console.log(`begin h = ${h}`);
      // When i == 0 this will be false
      if (h == i) {
        // when i == 1 and h == 1, this increments i to 2
        // on the next pass through the for loop that increments h,
        //     i == 2 and h == 2, this increments i to 3
        // so on, for each iteration of the inner loop, i gets incremented
        i++;
        console.log(`incremented i to ${i}`);
      }
      // this loop should happen just 14 times, 7 times for i = 0 and then 7
      // times starting with i = 1
      for (j = 0; j < 4; j++) {
        if (pieces.at(i).at(1) == -pieces.at(h).at(3)) {
          console.log(h);
          // exits the loop for j
          break;
          // unreachable, never happens
          console.log("never happens");
          i = h;
        } else {
          console.log(`shiftL(piecese.at(${h}))`);
          shiftL(pieces.at(h));
        }
      }
    }
  }

Hopefully this helps you figure out why this code is not working properly.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!