gpt4 book ai didi

javascript - FCC 在函数之外重构全局变量,数组未同时使用这两种方法

转载 作者:行者123 更新时间:2023-11-29 20:52:44 24 4
gpt4 key购买 nike

我正在研究新的 FCC Material 。肯定有部分问题。这个我已经接近了,但它仍然是错误的,因为它没有通过最终测试:

"newestBookList 应该等于 ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]"

当我使用 console.log(newestBookList) 时,我看到它删除了正确的条目但没有添加“Brief History”。有什么建议么?我不清楚为什么这两种方法在这里单独工作但不能一起工作,也不清楚如何使用 BDD 来测试...

// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function add(arr, bookName) {
return arr.concat([bookName]);
}

// Add your code above this line

/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function remove(arr, bookName) {
if (bookList.indexOf(bookName) >= 0) {
var index = bookList.indexOf(bookName);
return bookList.slice(0, index).concat(bookList.slice(index + 1));
// Add your code above this line
}
}

var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

//console.log(bookList);
//console.log(newBookList);
//console.log(newerBookList);
console.log(newestBookList);

最佳答案

您的remove 函数正在引用原始 bookList,而不是传递的数组。将所有 bookList 引用更改为 arr 引用(传递给 remove 函数的参数):

// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function add(arr, bookName) {
return arr.concat([bookName]);
}

// Add your code above this line

/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function remove(arr, bookName) {
if (arr.indexOf(bookName) >= 0) {
var index = arr.indexOf(bookName);
return arr.slice(0, index).concat(arr.slice(index + 1));
// Add your code above this line
} else console.log('n');
}

var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const added = add(bookList, 'A Brief History of Time');
var newestBookList = remove(added, 'On The Electrodynamics of Moving Bodies');

//console.log(bookList);
// console.log(newBookList);
//console.log(newerBookList);
console.log(newestBookList);

不过,您可能会考虑只检查一次 indexOf:

function remove(arr, bookName) {
var index = arr.indexOf(bookName);
if (index === -1) return;
return [...arr.slice(0, index), ...arr.slice(index + 1)];
}

关于javascript - FCC 在函数之外重构全局变量,数组未同时使用这两种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com