我在这个 Twilioquest 练习中做错了什么? (sortOrder.js - JavaScript 开始)
What am I doing wrong in this Twilioquest exercise? (sortOrder.js - JavaScript Begin)
嗯,这就是问题所在:
(TwillioQuest - Enable Beam 2 Problem)
Create a script called sortOrder.js. This script will take two command
line arguments - a pair of strings that should be compared to see
which one comes first alphabetically (letter casing is not important).
To test your script, you would execute it like this:
node sortOrder.js cats dogs
Your script should determine if the first string is before, after, or
in the same position (equal) to the second string, alphabetically. For
each case, you should print out a number with console.log as described
below.
When the first argument is earlier in the alphabet than the second, your script should print -1.
When the first argument is the same as the second, your script should print 0.
When the first argument is later in the alphabet than the second, your function should print 1.
基本上cats和dogs必须输出-1,dogs和cats输出1,dogs和dog输出0。必须忽略大小写。
然后,我尝试了两种不同的方法:
1- 创建两个列表(一个在排序方法之前,一个在排序方法之后)并使用 IF 顺序打印:
let aftSort = [];
let bfSort = [];
const first = process.argv[2].toLocaleLowerCase();
const second = process.argv[3].toLocaleLowerCase();
aftSort.push(first);
aftSort.push(second);
bfSort = [...aftSort];
aftSort.sort();
if (bfSort[0][0] == bfSort[1][0]){ //checking if the first letters are the same
console.log(0)};
if (bfSort[0] == aftSort[0]) { //checking if the first word is still first after sorting
console.log(-1)};
if(bfSort[0] == aftSort[1]){ //checking if the first word is the second after sorting
console.log(1)};
2- 创建相同的列表但在分析函数后仅打印一次:
let aftSort = [];
let bfSort = [];
const first = process.argv[2].toLocaleLowerCase();
const second = process.argv[3].toLocaleLowerCase();
aftSort.push(first);
aftSort.push(second);
bfSort = [...aftSort];
aftSort.sort();
function analyze(aftSort,bfSort) {
if (bfSort[0][0] == bfSort[1][0]){ //in case bothe initial letters are the same
console.log(0);
return;}
else {
if (bfSort[0] == aftSort[0]) { //in case the initial string is in the first position after sorting
console.log(-1);
return;} else if (bfSort[0] == aftSort[1]) { //in case the initial string is in the second place after sorting
console.log(1);
return;}
}
}
analyze(aftSort,bfSort);
编辑:现在,即使第三种方法只使用第一个字母而不使用列表也不起作用:
const firstValue = process.argv[2].toLowerCase();
const secondValue = process.argv[3].toLowerCase();
function analyze(firstValue, secondValue) {
if (firstValue[0][0] == secondValue[0][0]) {console.log(0)} //comparing first letters
else {
if (firstValue[0][0] < secondValue[0][0]) { //comparing the values
console.log(-1)}
else if (firstValue[0][0] > secondValue[0][0]){console.log(1)};
}
}
analyze(firstValue, secondValue)
然而,即使通过了我自己的测试,我仍然在游戏中收到此错误:
Your script must print "1" when the first argument passed to it
appears alphabetically later than the second argument.
有人可以帮忙吗?
再次尝试第三种方法,但不考虑只考虑第一个字母:
const sortFirstArg = process.argv[2].toLocaleLowerCase();
const sortSecondArg = process.argv[3].toLocaleLowerCase();
let answer = 0;
if (sortFirstArg < sortSecondArg){
answer = -1;
}
if (sortFirstArg > sortSecondArg){
answer = 1;
}
console.log(answer);
显然他们不想要真正的字母顺序...
编辑:这个有效。
试试这个:
const firstValue = process.argv[2] //保持这样。没有 .toLowerCase()
const secondValue = process.argv[3] //这里相同。没有 .toLowerCase()
让结果=0; //将结果存储在这里。
在 if 和 else if 中你可以比较 firstValue.tolowerCase() 和 secondValue.toLowerCase()
将您的变量 'result' 更新为 -1 或 0 或 1,具体取决于比较结果。
不要忘记在最后显示结果 console.log。
嗯,这就是问题所在:
(TwillioQuest - Enable Beam 2 Problem) Create a script called sortOrder.js. This script will take two command line arguments - a pair of strings that should be compared to see which one comes first alphabetically (letter casing is not important).
To test your script, you would execute it like this:
node sortOrder.js cats dogs
Your script should determine if the first string is before, after, or in the same position (equal) to the second string, alphabetically. For each case, you should print out a number with console.log as described below.
When the first argument is earlier in the alphabet than the second, your script should print -1.
When the first argument is the same as the second, your script should print 0.
When the first argument is later in the alphabet than the second, your function should print 1.
基本上cats和dogs必须输出-1,dogs和cats输出1,dogs和dog输出0。必须忽略大小写。
然后,我尝试了两种不同的方法:
1- 创建两个列表(一个在排序方法之前,一个在排序方法之后)并使用 IF 顺序打印:
let aftSort = [];
let bfSort = [];
const first = process.argv[2].toLocaleLowerCase();
const second = process.argv[3].toLocaleLowerCase();
aftSort.push(first);
aftSort.push(second);
bfSort = [...aftSort];
aftSort.sort();
if (bfSort[0][0] == bfSort[1][0]){ //checking if the first letters are the same
console.log(0)};
if (bfSort[0] == aftSort[0]) { //checking if the first word is still first after sorting
console.log(-1)};
if(bfSort[0] == aftSort[1]){ //checking if the first word is the second after sorting
console.log(1)};
2- 创建相同的列表但在分析函数后仅打印一次:
let aftSort = [];
let bfSort = [];
const first = process.argv[2].toLocaleLowerCase();
const second = process.argv[3].toLocaleLowerCase();
aftSort.push(first);
aftSort.push(second);
bfSort = [...aftSort];
aftSort.sort();
function analyze(aftSort,bfSort) {
if (bfSort[0][0] == bfSort[1][0]){ //in case bothe initial letters are the same
console.log(0);
return;}
else {
if (bfSort[0] == aftSort[0]) { //in case the initial string is in the first position after sorting
console.log(-1);
return;} else if (bfSort[0] == aftSort[1]) { //in case the initial string is in the second place after sorting
console.log(1);
return;}
}
}
analyze(aftSort,bfSort);
编辑:现在,即使第三种方法只使用第一个字母而不使用列表也不起作用:
const firstValue = process.argv[2].toLowerCase();
const secondValue = process.argv[3].toLowerCase();
function analyze(firstValue, secondValue) {
if (firstValue[0][0] == secondValue[0][0]) {console.log(0)} //comparing first letters
else {
if (firstValue[0][0] < secondValue[0][0]) { //comparing the values
console.log(-1)}
else if (firstValue[0][0] > secondValue[0][0]){console.log(1)};
}
}
analyze(firstValue, secondValue)
然而,即使通过了我自己的测试,我仍然在游戏中收到此错误:
Your script must print "1" when the first argument passed to it appears alphabetically later than the second argument.
有人可以帮忙吗?
再次尝试第三种方法,但不考虑只考虑第一个字母:
const sortFirstArg = process.argv[2].toLocaleLowerCase();
const sortSecondArg = process.argv[3].toLocaleLowerCase();
let answer = 0;
if (sortFirstArg < sortSecondArg){
answer = -1;
}
if (sortFirstArg > sortSecondArg){
answer = 1;
}
console.log(answer);
显然他们不想要真正的字母顺序...
编辑:这个有效。
试试这个:
const firstValue = process.argv[2] //保持这样。没有 .toLowerCase()
const secondValue = process.argv[3] //这里相同。没有 .toLowerCase()
让结果=0; //将结果存储在这里。
在 if 和 else if 中你可以比较 firstValue.tolowerCase() 和 secondValue.toLowerCase()
将您的变量 'result' 更新为 -1 或 0 或 1,具体取决于比较结果。
不要忘记在最后显示结果 console.log。