Find out Word Square from given Array

Question: Find out the word square from given array.

Answer:

function isWordSquare(input) {

const numRow = input.length;

const numCol = numRow ? input[0].length : 0;

if (!numRow) return false;

if (!numCol) return true;

if (numRow !== numCol) return false;

for (let i = 0; i < numRow; i++) {

for (let j = 0; j < numCol; j++) {

if (input[i][j] !== input[j][i]) return false;

}

}

return true;

}

Try it yourself: