[英]Overall function doesn't run even though console.log gives proper result
Pig Latin assignment. 猪拉丁作业。 Able to get correct return to print via console.log, but script returns undefined if run in the node.js test. 可以通过console.log获得正确的打印返回值,但是如果在node.js测试中运行,脚本将返回未定义的返回值。
need to debug before moving forward. 在继续之前需要调试。
tried moving the final function call elsewhere in the script. 尝试将最终函数调用移至脚本中的其他位置。 i think maybe it is because of scoping, but this is only my second JS project so i'm really a newbie. 我认为可能是因为范围界定,但这只是我的第二个JS项目,所以我真的是一个新手。
function pigLatin(word) {
// Global variables
const vowels = ["a", "e", "i", "o", "u"];
const splitWord = word
.toLowerCase()
.trim()
.split("");
// Slice Word at first Vowel to end
function firstPart(param1, param2) {
for (let v = 0; v < vowels.length; v++) {
for (let w = 0; w < splitWord.length; w++) {
if (vowels[w] === splitWord[v]) {
return `${splitWord.slice(v, splitWord.length).join("")}`;
} else if (splitWord.length === 1) {
return `${""}`;
}
}
}
}
// Slice word from First Letter to Vowel & if first letter is vowel
function secondPart(param1, param2) {
for (let v = 0; v < vowels.length; v++) {
for (let w = 0; w < splitWord.length; w++) {
if (vowels[w] === splitWord[0]) {
return `${splitWord.splice([0], [v]).join("")}yay`;
} else if (vowels[w] === splitWord[v]) {
return `${splitWord.splice([0], [v]).join("")}ay`;
} else if (splitWord.length === 1) {
return `${splitWord.join("")}yay`;
}
}
}
}
// Combine returns from firstPart and secondPart
const result1 = firstPart(vowels, splitWord);
const result2 = secondPart(vowels, splitWord);
function combine(param1, param2) {
return `${param1}${param2}`;
}
console.log(combine(result1, result2));
}
expected results are to have the script run successfully. 预期结果是使脚本成功运行。 but when combine(result1, result2) is called outside of console.log, script return undefined. 但是当在console.log外部调用combin(result1,result2)时,脚本返回未定义。
Your 'word' is undefined in 您的“单词”在中未定义
const splitWord = word
.toLowerCase()
.trim()
.split("");
add by example 通过示例添加
var word = 'test';
on the first rule of the script to get it working for a start 在脚本的第一条规则上使其开始工作
Edit: this is if you forget the word in the first place when calling the function :) 编辑:这是如果您在调用函数时首先忘记单词:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.