I'm trying to solve a programing puzzle/game.
It's a Spiral Matrix and the result is correct but my array is outputting .0
at the end of my arrays.
For example, the input is
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
The output is
[1.0,2.0,3.0,6.0,9.0,8.0,7.0,4.0,5.0]
But I'm looking for
[ 1, 2, 3, 6, 9, 8, 7, 4, 5]
The code I am using is from the YouTube video "LeetCode 54 Spiral Matrix in javascript".
How do I Logger.log(result)
without the .0
?
function myFunction() {
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// result array
const result = [];
// boundry varibals
let left = 0;
let top = 0;
let right = matrix[0].length - 1;
let bottom = matrix.length - 1;
// starting direction clock wise
let direction = 'right';
// start loop
while (left <= right && top <= bottom) {
if (direction === 'right') {
for (let i = left; i <= right; i = 1) {
result.push(matrix[top][i]);
}
top = 1;
direction = 'down';
} else if (direction === 'down') {
for (let i = top; i <= bottom; i = 1) {
result.push(matrix[i][right]);
}
right -= 1;
direction = 'left';
} else if (direction === 'left') {
for (let i = right; i >= left; i -= 1) {
result.push(matrix[bottom][i]);
}
bottom -= 1;
direction = 'up';
} else if (direction === 'up') {
for (let i = bottom; i >= top; i -= 1) {
result.push(matrix[i][left]);
}
left = 1;
direction = 'right';
}
}
Logger.log(result);
return result;
}