简体   繁体   English

为什么输出显示未定义,我该如何摆脱它?

[英]Why the output shows undefined and how can I get rid of it?

I have 3 functions:我有3个功能:

  1. reverseCharacter --> reversing characters reverseCharacter --> 反转字符
  2. checkType --> checking type String or Number and reversing it checkType --> 检查类型 String 或 Number 并将其反转
  3. newFunction --> runs through the arrays (arrayTest1, arrayTest2 and arrayTest3), reversing and checking the type of the characters newFunction --> 遍历数组(arrayTest1、arrayTest2 和 arrayTest3),反转并检查字符的类型
function reverseCharacters(stringToReverse){ return stringToReverse.split('').reverse().join(''); } function checkType(stringToCheck){ if(typeof (stringToCheck) === 'string'){ console.log(reverseCharacters(stringToCheck)); } else if(typeof (stringToCheck) === 'number'){ console.log(reverseCharacters(String(stringToCheck))); } } let arrayTest1 = ['apple', 'potato', 'Capitalized Words'].reverse(); let arrayTest2 = [123, 8897, 42, 1168, 8675309].reverse(); let arrayTest3 = ['hello', 'world', 123, 'orange'].reverse(); function newFunction(x){ for(let i = 0; i <= x.length; i++){ console.log(checkType(x[i])); } } newFunction(arrayTest3); newFunction(arrayTest2); newFunction(arrayTest1);

When I run the code I get undefined after each value:当我运行代码时,我在每个值后都未定义:

egnaro
undefined
321
undefined
dlrow
undefined
olleh
undefined
undefined
9035768
undefined
8611
undefined
24
undefined
7988
undefined
321
undefined
undefined
sdroW dezilatipaC
undefined
otatop
undefined
elppa
undefined
undefined

My question is why it says undefined in the output and how can I get rid of it?我的问题是为什么它在输出中说未定义,我该如何摆脱它?

Your function has no explicit return statement, therefore it's return value is undefined.您的函数没有明确的 return 语句,因此它的返回值是未定义的。 You can prove this by adding return "see - no more undefined";您可以通过添加 return "see - no more undefined" 来证明这一点; as the last line of the checkType() function.作为checkType()函数的最后一行。

Or, simply remove the console.log() part of the call to checkType()或者,只需删除调用checkType()console.log()部分

 function reverseCharacters(stringToReverse){ return stringToReverse.split('').reverse().join(''); } function checkType(stringToCheck){ if(typeof (stringToCheck) === 'string'){ console.log(reverseCharacters(stringToCheck)); } else if(typeof (stringToCheck) === 'number'){ console.log(reverseCharacters(String(stringToCheck))); } return 'See, no more undefined'; } let arrayTest1 = ['apple', 'potato', 'Capitalized Words'].reverse(); let arrayTest2 = [123, 8897, 42, 1168, 8675309].reverse(); let arrayTest3 = ['hello', 'world', 123, 'orange'].reverse(); function newFunction(x){ for(let i = 0; i <= x.length; i++){ console.log(checkType(x[i])); } } newFunction(arrayTest3); newFunction(arrayTest2); newFunction(arrayTest1);

Just rewrite your newFunction() like this只需像这样重写你的newFunction()

 function newFunction(x){ for(let i = 0; i <= x.length; i++){ //console.log(checkType(x[i])); checkType(x[i]) } }

The error is happening because you were trying to console.log() value from checkType() .发生错误是因为您试图从checkType()获取console.log()值。 Since checkType() does not have any return value, it just logs reverseCharacters() return values, you are getting undefined printed.由于checkType()没有任何返回值,它只记录reverseCharacters()返回值,您将得到 undefined 打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM