OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Getting the wrong output can anyone let me know what's the problem in my code [closed]

  • Thread starter Thread starter Prashant Shukla
  • Start date Start date
P

Prashant Shukla

Guest
Problem Description A matrix and a valid current position (row number and column number) are provided as input. Given a direction (RIGHT is 1, DOWN in 2, LEFT is 3 and UP is 4) and number of steps to move, output the values along the path taken to the new position, including the value at the new position.

If any position during traversal is out of bounds, output a single -1 and not the values along the path.

Code:
function matrixTraversal(matrix, currPosRow, currPosCol, dirToMove, stepsToMove) {
    const M = matrix.length;
    const N = matrix[0].length;

    let x = currPosRow, y = currPosCol;

    // Directions: RIGHT, DOWN, LEFT, UP
    const dx = [0, 1, 0, -1];
    const dy = [1, 0, -1, 0];

    let result = [];

    // Check if the starting position is out of bounds
    if (x < 0 || x >= M || y < 0 || y >= N) {
        return "-1";
    }

    // Traverse for the given number of steps
    for (let step = 0; step <= stepsToMove; step++) {
        if (x < 0 || x >= M || y < 0 || y >= N) {
            return "-1";
        }
        result.push(matrix[x][y].toString());

        x += dx[dirToMove - 1];
        y += dy[dirToMove - 1];
    }

    return result.join(" ");
}

tried rewrite and everytime getting the wrong output

<p>Problem Description
A matrix and a valid current position (row number and column number) are provided as input. Given a direction (RIGHT is 1, DOWN in 2, LEFT is 3 and UP is 4) and number of steps to move, output the values along the path taken to the new position, including the value at the new position.</p>
<p>If any position during traversal is out of bounds, output a single -1 and not the values along the path.</p>
<pre class="lang-js prettyprint-override"><code>function matrixTraversal(matrix, currPosRow, currPosCol, dirToMove, stepsToMove) {
const M = matrix.length;
const N = matrix[0].length;

let x = currPosRow, y = currPosCol;

// Directions: RIGHT, DOWN, LEFT, UP
const dx = [0, 1, 0, -1];
const dy = [1, 0, -1, 0];

let result = [];

// Check if the starting position is out of bounds
if (x < 0 || x >= M || y < 0 || y >= N) {
return "-1";
}

// Traverse for the given number of steps
for (let step = 0; step <= stepsToMove; step++) {
if (x < 0 || x >= M || y < 0 || y >= N) {
return "-1";
}
result.push(matrix[x][y].toString());

x += dx[dirToMove - 1];
y += dy[dirToMove - 1];
}

return result.join(" ");
}
</code></pre>
<p>tried rewrite and everytime getting the wrong output</p>
 
Top