我在 JavaScript 测试中遇到困难,我的答案没有通过,但我不明白为什么?
问题是:
Write a function that returns the reverse of a given string. Although many languages have a built in function to reverse a string, you should do it manually, building up the output character by character. For example, "Hello" returns "olleH"
这就是我到目前为止所拥有的......
stringReverse = function(input) {
// Your code goes here
var inputA = input.split('');
var inputL = inputA.length;
var beta = new Array(inputL);
for (var i=0; i<inputL; i++) {
var alpha = inputA.pop();
beta.splice(i,1,alpha);
}
var newStr = beta.join('');
return console.log(newStr);
};
我的输出看起来正确,但它没有通过测试。我收到以下消息:
Started
olleH FA F F Failures: 1) SolutionTests Test1 Message: Expected undefined to equal 'olleH'. Stack: Error: Expected undefined to equal 'olleH'. at Object. (/sandboxes/deer/SolutionSpec.js:7:40) 2) SolutionTests Test2 Message: Expected undefined to equal 'A'. Stack: Error: Expected undefined to equal 'A'. at Object. (/sandboxes/deer/SolutionSpec.js:11:36) 3) SolutionTests Test3 Message: Expected undefined to equal ''. Stack: Error: Expected undefined to equal ''. at Object. (/sandboxes/deer/SolutionSpec.js:15:35) 3 specs, 3 failures Finished in 0.014 seconds
最佳答案
return console.log(newStr);
这将返回未定义
值。我认为你的意思是:
return newStr;
关于javascript - 反转 JavaScript 中的字符串 : Why is my answer not passing the test?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523050/