I made a code that emulates the behavior of the readline()
function in This CodinGame exercise.
The code is the following:
function* cutie(txt){
const x=txt.split("n")
const w=x.reduce((a,b)=>b.length>a?b.length:a,0)
yield x.length+" "+w
for(const i of x){yield i}
}
function readline(){
return XX.next().value
}
XX=cutie(`##########
#S.......#
##.#####.#
##.#.....#
##########`)
For some strange reason that is beyond me, it works in Firefox’s console (when calling separately), and in Node.js v16.20.2, but it still gives undefined
in the following code:
function* cutie(txt) {
const x = txt.split("n")
const w = x.reduce((a, b) => b.length > a ? b.length : a, 0)
yield x.length + " " + w
for (const i of x) {
yield i
}
}
function readline() {
return XX.next().value
}
XX = cutie(`##########
#S.......#
##.#####.#
##.#.....#
##########`)
function setNb(x, y, val) {
if (grid[y][y] === null) {
grid[y][x] = val
} else {
grid[y][x] = val < grid[y][x] ? val : grid[y][x]
}
if (y - 1 > 0 && grid[y - 1][x] === null) setNb(x, y - 1, val + 1)
if (x - 1 > 0 && grid[y][x - 1] === null) setNb(x - 1, y, val + 1)
if (y + 1 < grid.length && grid[y + 1][x] === null) setNb(x, y + 1, val + 1)
if (x + 1 < grid[y].length && grid[y][x + 1] === null) setNb(x + 1, y, val + 1)
}
var inputs = readline().split(' ');
const w = parseInt(inputs[0]);
const h = parseInt(inputs[1]);
let grid = [],
start
for (let i = 0; i < h; i++) {
let b = readline()
grid.push(b.split("").map(a => a == "." ? null : a))
if (b.indexOf("S") != -1) {
start = {
x: b.indexOf("S"),
y: i
}
}
}
setNb(start.x, start.y, 0)
console.log(grid.map(a => a.map(b => isNaN(b) ? b : b.toString(36).toUpperCase()).join("")).join("n"))
The code will not run because I made a mistake in the recursion. But in the Firefox console on an about:blank
tab, the code dies before it could reach the recursion.
What could be the issue?