【发布时间】: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) === 1和checkIfContainsHonorific(name)是假的……那只是其中之一 -
谢谢大家!我敢肯定它只是在某个地方差了一个!我很欣赏最小的可重现示例链接,下次我一定会遵循。抱歉今天在这里贴了这么多。
标签: javascript arrays function higher-order-functions