function inRow(array, number)
{
if (number > array.length || number < 1 || !Number.isInteger(number))
{
return false;
}
let count = 0;
for (let i = 0; i < array.length; i++)
{
if (array[i][i] === 1)
{
count++;
}
else
{
count = 0;
}
}
if (count >= number)
{
return true;
}
else
{
count = 0;
for (let i = (array.length - 1); i >= 0; i--)
{
if (array[i][array.length - 1 - i] === 1)
{
count++;
}
else
{
count = 0;
}
}
if (count === number)
{
return true;
}
count = 0;
for (let i = 0; i < array.length; i++)
{
for (let k = 0; k < array.length; k++)
{
if (array[i][k] === 1)
{
count++;
}
else
{
count = 0;
}
}
if (count === number)
{
return true;
}
else
{
count = 0;
}
}
count = 0;
for (let i = 0; i < array.length; i++)
{
for (let k = 0; k < array.length; k++)
{
if (array[k][i] === 1)
{
count++;
}
else
{
count = 0;
}
}
if (count === number)
{
return true;
}
else
{
count = 0;
}
}
}
return false;
}
n = 2
[0,1,1] => true
[1,1,0] => false
const arr = [
[0,0,0,0],
[1,0,0,0],
[0,1,0,0],
[0,0,0,0],
];
const n = 2;
const inRow = (arr, oneMax) => {
const { length } = arr;
if(oneMax > length) {
return false;
}
const oneCounter = {
'--': 0,
'||': 0,
'\\': {
top: 0,
bot: 0,
},
'//': {
top: 0,
bot: 0,
},
};
const check = (value, counter, key) => {
if(value === 1) {
counter[key]++;
if(counter[key] === oneMax) {
return true;
}
} else {
counter[key] = 0;
}
return false;
}
for(let i = 0; i < length; i++) {
for(let j = 0; j < length; j++) {
if(
check(arr[i][j], oneCounter, '--') ||
check(arr[j][i], oneCounter, '||')
) {
return true;
}
}
oneCounter['--'] = 0;
oneCounter['||'] = 0;
}
const maxDiags = length - n + 1;
const last = length - 1;
for(let i = 0, maxJ = length; i < maxDiags; i++, maxJ--) {
for(let j = 0; j < maxJ; j++) {
if(
check(arr[j][i + j], oneCounter['\\'], 'top') ||
check(arr[i + j][j], oneCounter['\\'], 'bot') ||
check(arr[j][last - (i + j)], oneCounter['//'], 'top') ||
check(arr[i + j][last - j], oneCounter['//'], 'bot')
) {
return true;
}
}
oneCounter['\\'].top = 0;
oneCounter['\\'].bot = 0;
oneCounter['//'].top = 0;
oneCounter['//'].bot = 0;
}
return false;
}
inRow(arr, n)