简体   繁体   English

即使console.log提供正确的结果,整体功能也无法运行

[英]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.

相关问题 即使之前的 console.log 显示该语句应该运行,if 语句也不会运行 - if statement doen't run even though the console.log before that shows the statement should run 为什么单击按钮时会执行两个console.log,即使它们不在函数内 - Why are the two console.log executed when I click the button even though they aren't inside the function console.log(variable); VS console.log([variable]); /一个提供信息,另一个不提供? - console.log( variable ); VS console.log( [variable] ); / One gives info the other doesn't? 等待后,console.log 不返回异步结果 - console.log doesn't return result in async after await Console.log在异步功能中不起作用 - Console.log doesn't work in async function 函数看不到 Object 属性,但 console.log 看不到 - Function doesn't see Object property, but console.log does console.log 不会在异步 function 中等待“等待” - console.log doesn't wait "await" in async function javascript函数即使在console.log中显示值也返回未定义 - javascript function returning undefined even though value display in console.log React state 未定义,即使 console.log 显示它 - React state is undefined , even though console.log shows it JS 说 object 是未定义的,即使它显示在 console.log 中 - JS says an object is undefined even though it shows with console.log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM