首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FCC将全局变量从函数中重构出来,数组没有同时使用这两种方法

FCC将全局变量从函数中重构出来,数组没有同时使用这两种方法
EN

Stack Overflow用户
提问于 2018-06-22 03:27:30
回答 1查看 1.3K关注 0票数 2

通过新的FCC材料。肯定出了点问题。这个我已经接近了,但是它仍然是错误的,因为它没有通过最后的测试:

"newestBookList应等同于“巴斯克维尔猎犬”、“哲学家”、“亚里士多德研究”、“时间简史”

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

代码语言:javascript
复制
// 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-22 03:34:39

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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
function remove(arr, bookName) {
  var index = arr.indexOf(bookName);
  if (index === -1) return;
  return [...arr.slice(0, index), ...arr.slice(index + 1)];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50980230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档