I’m am very new to programming and trying to salve 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.
example
input is [ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]
output is [1.0,2.0,3.0,6.0,9.0,8.0,7.0,4.0,5.0]
looking for [ 1, 2, 3, 6, 9, 8, 7, 4, 5]
the code I am using is from this 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;
}
I hope this question is simple I don’t know what else I should add the puzzle is from an indie game BitRunner it got me more into programming than I use to be in adding this info at the end because the post won’t submit if it has more code then words