javaScript:如何检查参数是否与数组中的最后一个字符串匹配

时间:2018-05-30 23:43:43

标签: javascript arrays

如何检查传入nameSearch的参数是否与names数组中的最后一项匹配?

function nameSearch(name) {
  var names = ["lopi", "aja", "lombo", "dakes", "polq", "kinto", "lufe"];
  var i = 0;

  while (i < names.length) {
    if (names[i] !== name) {
      console.log("The current name is " + names[i]);

      if (name === names.indexOf(names.length - 1)) { //option one,did not work
        if (name === names.indexOf("lufe")) { //option two hard coded,still did not work
          console.log("This is the last name in the directory");
        }

      } else {
        console.log("We found " + names[i] + " he is number " + i);
        break;

      }

      i++;
    }

  }
  nameSearch("lufe");

0 个答案:

没有答案