所以这是一个小型的终端游戏,剪刀石头布的,我需要编写其余代码的帮助,以便它可以通过我的节点测试。我已经能够编写代码并输入hand1和hand2的决策,并获得显示谁获胜的结果,但是当我运行它们时,它似乎并没有通过节点测试。
节点测试将“领带”检测为正确的,但未检测出获胜的手或擦洗输入为正确的,但是当我在终端中使用它时,所有操作均有效。这是节点测试所说的图像
需要重写的代码在下面的函数rockPaperScissors(hand1,hand2)中
'use strict';
const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function rockPaperScissors(hand1, hand2) {
if (hand1 === hand2) {
return "It's a tie!";
} else if (hand1 === 'rock') {
if (hand2 === 'paper') {
return "Hand two wins!";
} else if (hand2 === 'scissors') {
return "Hand one wins!";
}
}
if (hand1 === 'paper') {
if (hand2 === 'rock') {
return "Hand one wins!";
} else if (hand2 === 'scissors') {
return "Hand two wins!";
}
}
if (hand1 === 'scissors') {
if (hand2 === 'paper') {
return "Hand one wins!";
} else if (hand2 === 'rock') {
return "Hand two wins!";
}
}
}
function getPrompt() {
rl.question('hand1: ', (answer1) => {
rl.question('hand2: ', (answer2) => {
console.log( rockPaperScissors(answer1, answer2) );
getPrompt();
});
});
}
// Tests
if (typeof describe === 'function') {
describe('#rockPaperScissors()', () => {
it('should detect a tie', () => {
assert.equal(rockPaperScissors('rock', 'rock'), "It's a tie!");
assert.equal(rockPaperScissors('paper', 'paper'), "It's a tie!");
assert.equal(rockPaperScissors('scissors', 'scissors'), "It's a tie!");
});
it('should detect which hand won', () => {
assert.equal(rockPaperScissors('rock', 'paper'), "Hand two wins!");
assert.equal(rockPaperScissors('paper', 'scissors'), "Hand two wins!");
assert.equal(rockPaperScissors('rock', 'scissors'), "Hand one wins!");
});
it('should scrub input to ensure lowercase with "trim"ed whitepace', () => {
assert.equal(rockPaperScissors('rOcK', ' paper '), "Hand two wins!");
assert.equal(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!");
assert.equal(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!");
});
});
} else {
getPrompt();
}
您必须修剪和小写hand1
和hand2
参数的值:
function rockPaperScissors(hand1, hand2) {
hand1 = hand1.trim().toLowerCase();
hand2 = hand2.trim().toLowerCase();
...
该装饰功能从文本中删除多余的空格。toLowerCase函数将所有字符转换为小写。
现在,您的rockPaperScissors
函数如下所示:
function rockPaperScissors(hand1, hand2) {
hand1 = hand1.trim().toLowerCase();
hand2 = hand2.trim().toLowerCase();
if (hand1 === hand2) {
return "It's a tie!";
} else if (hand1 === 'rock') {
if (hand2 === 'paper') {
return "Hand two wins!";
} else if (hand2 === 'scissors') {
return "Hand one wins!";
}
}
if (hand1 === 'paper') {
if (hand2 === 'rock') {
return "Hand one wins!";
} else if (hand2 === 'scissors') {
return "Hand two wins!";
}
}
if (hand1 === 'scissors') {
if (hand2 === 'paper') {
return "Hand one wins!";
} else if (hand2 === 'rock') {
return "Hand two wins!";
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句