微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在这个 Twilioquest 练习中做错了什么? sortOrder.js - JavaScript 开始

如何解决我在这个 Twilioquest 练习中做错了什么? sortOrder.js - JavaScript 开始

好吧,这就是问题所在:

twillioQuest - 启用 Beam 2 问题) 创建一个名为 sortOrder.js 的脚本。此脚本将采用两个命令 行参数 - 应该比较以查看的一对字符串 按字母顺序排在第一位(字母大小写不重要)。

要测试您的脚本,您可以像这样执行它:

node sortOrder.js cats dogs

您的脚本应该确定第一个字符串是在之前、之后还是之后 在相同的位置(等于)第二个字符串,按字母顺序。为了 在每种情况下,您都应该按照说明使用 console.log 打印出一个数字 下面。

  • 当第一个参数在字母表中早于第二个时,您的脚本应打印 -1。

  • 当第一个参数与第二个参数相同时,您的脚本应该打印 0。

  • 当第一个参数在字母表中晚于第二个时,您的函数应该打印 1。

基本上猫狗必须输出-1,狗和猫输出1,狗和狗输出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)

然而,即使通过了我自己的测试,我仍然在游戏中收到此错误

当第一个参数传递给脚本时,您的脚本必须打印“1” 按字母顺序出现在第二个参数之后。

有人可以帮忙吗?

解决方法

再次尝试第三种方法,但不只考虑第一个字母:

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()

根据比较结果将变量“结果”更新为 -1、0 或 1。

不要忘记在最后用 console.log 显示结果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。