【问题标题】:Getting undefined, and can't find the bug未定义,找不到错误
【发布时间】:2021-03-07 01:39:22
【问题描述】:

我无法在我的程序中找到错误。标有“Debug 3”和“Debug 6”的 console.logs 都返回 undefined。我敢肯定,这很明显,但任何指出他们的帮助将不胜感激。我还是新手,所以如果您对我应该这样做的不同方式有任何建议,我愿意接受反馈!

我知道我可以只写一堆 if/else 语句,但我真的很想诚实地尝试使代码模块化,并尽可能地可重用。

旁注:我知道我的函数/变量名称是垃圾。一旦程序正常运行,我计划将它们全部重命名。这更容易帮助跟踪(尽管显然效果不佳,因此我在这里)。

我目前的代码是:

// ---------------------- INPUT CHECKS ---------------------- //

// Check and return the number of words supplied in the name
const checkNameLength = (name) => {
  arr = name.split(' ');
  return arr.length;
};


// Checks if the name contains any spaces on the edges
const checkForSpacesOnEdges = (name) => {
  if (name[0] === ' ' || name[-1] === ' ') {
    return true;
  } else {
    return false;
  }
};


// Checks if the name contains an honorific
const checkIfContainsHonorific = (name) => {
  let accumulator = 0;
  let result;
  for (let i = 0; i < name.length; i++) {
    if (name[i] === '.') {
      accumulator++;
    }
  }
  if (accumulator !== 0) {
    result = true;
  } else {
    result = false;
  }
  return result;
};


// Returns a 1 word string 
const isSingleName = (name) => {
  let result;
  let arr = name.split(' ');
  if (arr.length === 1) {
    result = true;
  } else {
    result = false;
  }
  return result;
};


// ---------------------- OUTPUT ---------------------- //

// Return empty string
const emptyOutput = (name) => {
  return 'empty string';
};


// Returns the exact string provided by user
const returnAsIs = (name) => {
  return name;
};


// Returns the name with trailing spaces removed
const returnWithoutSpaces = (name) => {
  return name.trim();
};


// Reverses the name string and returns it
const normalReverse = (name) => {
  let arr = [];
  let result;
  arr = name.split(' ');
  arr.reverse();
  result = arr[0] + ', ' + arr[1];
  result.toString();
  return result;
};


// Reverses the first and last name, but leaves the honorific in front
const hasHonorificReverseNames = (name) => {
  let arr = name.split(' ');
  let firstAndLastArr = [];
  for (let i = 1; i < arr.length; i++) {
    firstAndLastArr.push(arr[i]);
  }
  firstAndLastArr.reverse();
  return arr[0].toString() + firstAndLastArr[0].toString() + ', ' + firstAndLastArr[1].toString();
};


// Main func
const nameInverter = (name) => {
  let result;
  if (!name) {
    result = emptyOutput(name);

  } else if (isSingleName(name)) {
    result = returnAsIs(name);
    
  } else if (!checkIfContainsHonorific(name)) {
    if (checkForSpacesOnEdges(name)) {
      result = returnWithoutSpaces(name);
    }

  } else if (checkNameLength(name) === 1) {
    if (checkIfContainsHonorific(name)) {
      result = emptyOutput(name);
    }
    
  } else if (checkNameLength(name) === 2) {
    console.log("Flag 1: ", name);
    if (!checkIfContainsHonorific(name)) {
      console.log("Flag 2: ", name);
      result = name;
    }
    
    
  } else if (checkNameLength(name) === 2) {
    if (checkIfContainsHonorific(name)) {
      result = returnAsIs(name);
    }
    
  } else if (checkNameLength(name) === 3) {
    if (checkIfContainsHonorific(name)) {
      result = hasHonorificReverseNames(name);
    }

  } else {
    return normalReverse(name);
  }
  return result;
};


console.log("Debug 1", nameInverter(''));
console.log("Debug 2", nameInverter('Ronald'));
console.log("Debug 3", nameInverter('Ronald McDonald'));
console.log("Debug 4", nameInverter(' Ronald McDonald '));
console.log("Debug 5", nameInverter('Dr. Ronald McDonald'));
console.log("Debug 6", nameInverter('Dr. Ron'));

我从这些 console.logs 获得的输出(包括我怀疑导致问题的函数中的 Flag 1 console.log,如下所列:

Debug 1 empty string
Debug 2 Ronald
Debug 3 undefined
Debug 4 Ronald McDonald
Debug 5 Dr.McDonald, Ronald
Flag 1:  Dr. Ron
Debug 6 undefined

谢谢,非常感谢任何指导!

【问题讨论】:

  • 我敢打赌你在某个地方落后了...
  • 尝试将代码缩减为minimal reproducible example。代码太多了。
  • 另外,隐式全局变量很危险。
  • 在您的 nameInverter 函数中存在结果未定义的情况 - 例如如果 checkNameLength(name) === 1checkIfContainsHonorific(name) 是假的……那只是其中之一
  • 谢谢大家!我敢肯定它只是在某个地方差了一个!我很欣赏最小的可重现示例链接,下次我一定会遵循。抱歉今天在这里贴了这么多。

标签: javascript arrays function higher-order-functions


【解决方案1】:

所以你的问题是你实际上到达了你的一些if else 语句,但是没有在那个代码块中设置代码。

任何时候你在这里得到undefined,是因为你没有将result设置为任何东西。

if else 语句不会“失败”,因此只要一个为真,它将运行该代码块,但不会运行以下代码块。

一个很好的例子是你的'debug 3''Ronald Mcdonald'检查。它进入您的 if else 语句,并命中 has length === 2 测试,但随后在该代码块中,您执行另一个测试以查看它是否包含“博士”。如果确实如此,则将结果设置为名称,但如果此检查失败,则永远不会将结果设置为名称。

基本上,您要么需要对 if else 语句进行排序,以便它们可以按顺序检查并且它们不会发生冲突,要么您需要在这些检查中嵌套额外的 if else 语句以检查您想要的其他内容到。

【讨论】:

  • 谢谢!再加上我已经收到的反馈,这非常有帮助。我刚刚错过了很多步骤/路径。我将返回并再次遍历所有内容,并确保没有任何死胡同。
猜你喜欢
  • 2022-01-18
  • 2012-07-27
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 2016-04-11
  • 2018-11-26
相关资源
最近更新 更多