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

99
Views
Nested matrix and nested loop problem not working

This should be the input and output: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] The returned output is actually the same as the input. What am I doing wrong?

var rotate = function(matrix) {
        let result = matrix;
        let i = 0;
        let index = matrix.length - 1;
        for (let x of matrix) {
            for (let n of x) {
                result[i][index] == n;
                if (i<matrix.length-2)
                i++;    
            }
            index--;
        }
        console.log(result);
        return result;
        };
        
        var list= [[1,2,3],[4,5,6],[7,8,9]];
        
        rotate(list);

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

0

const matrix = [
  [1,2,3],
  [4,5,6],
  [7,8,9]
];

const rotate = (m) => {
  const n = m.length;
  const r = [ ...m.map(a => []) ];
  
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      r[i][j] = m[n - j - 1][i];
    }
  }
  return r;
}

const result = rotate(matrix);
console.log(result);

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!