简体   繁体   English

return 语句在 javascript 中不返回任何内容

[英]return statement returns nothing in javascript

Given a square grid of characters in the range ascii[az], rearrange elements of each row alphabetically, ascending.给定范围 ascii[az] 中的一个方形字符网格,按字母顺序重新排列每行的元素,升序。 Determine if the columns are also in ascending alphabetical order, top to bottom.确定列是否也按字母升序排列,从上到下。 Return YES if they are or NO if they are not.如果是则返回 YES,否则返回 NO。

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM