Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not.
const gridOrig = ["ebacd", "fghij", "olmkn", "trpqs", "xywuv"];
function gridChallenge(grid) {
// Write your code here
let gridOne = [];
let gridTwo = [];
for (let i = 0; i < grid.length; i++) {
gridOne.push(grid[i].split(""));
// console.log(gridOne);
}//this section makes each element of the array become array
for (let i = 0; i < gridOne.length; i++) {
gridTwo.push(gridOne[i].sort());
// console.log(gridTwo);
}//this section sort each element of the parent array
let answer = [];
for (let i = 0; i < gridTwo.length - 1; i++) {
for (let j = 0; j < gridTwo[i].length; j++) {
if (gridTwo[i][j] <= gridTwo[i + 1][j]) {
// debugger;
// console.log("true");
answer.push("YES");
} else if (undefined) {
answer = "NO";
} else {
// console.log("false");
answer.push("NO");
}
}
}
// console.log(answer);
let finalAns = "NO";
if (answer.includes("NO")) {
finalAns = "NO";
} else {
finalAns = "YES";
}
console.log(finalAns); //I can see it on the console
return finalAns; // this returns nothing
}
gridChallenge(gridOrig);
It does return the result, you just don't use it. Try this:
const gridChallengeResult = gridChallenge(gridOrig); // saving returned value to variable
console.log(gridChallengeResult) // logging it to the console