Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
return statement returns nothing in javascript

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);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!