1.重构函数中的全局变量

满足要求:

bookListshould not change and still equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"].newBookListshould equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"].newerBookListshould equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"].newestBookListshould equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"].

重构(重写)代码,函数内部不更改全局数组。add函数应将给定值添加bookName到数组的末尾。

remove函数应该bookName从数组中删除给定的值。

这两个函数都应该返回一个数组,并且应该在该数组之前添加新参数bookName

// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];// New parameters should come before the bookName one
// Add your code below this line
function add (bookName) {return bookList.push(bookName);
// Add your code above this line
}// New parameters should come before the bookName one
// Add your code below this line
function remove (bookName) {if (bookList.indexOf(bookName) >= 0) {return bookList.splice(0, 1, bookName);}
// 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);

对于add函数,在不改变bookList前提下,用.concat()为被连接数组连接数组,并且返回被连接数组的副本(不改变原数组)

function add (bookList,bookName) {
return bookList.concat(bookName);
}

remove函数,用filter(在某种程度上过滤=remove)返回一个新数组

function remove (bookList, bookName) {return bookList.filter(b=>b!==bookName);
}

扩展

concat()

var a=[1,2];
var b=["matalang","mixian"];
console.log(a.concat(4,6));//[1,2,4,6]
console.log(a.concat(b));

filter()

//求偶
console.log([2,7].filter(a=>a%2===0));

reduce()

[21,4].reduce((prev,curr,index)=>{
return prev+curr;
})

map()

console.log([12,22].map(a=>a/2));
//[6,11]
var watchList = [{  "Title": "Inception","imdbRating": "8.8","imdbVotes": "1,446,708",},{  "Title": "Interstellar","imdbRating": "8.6","imdbVotes": "910,366",},{"Title": "The Dark Knight","imdbRating": "9.0","imdbVotes": "1,652,832",},{  "Title": "Batman Begins","imdbRating": "8.3","imdbVotes": "972,584",}];var rating = [];
rating=watchList.map(a=>({title: a["Title"],
rating: a["imdbRating"]}));
//rating=watchList.map((index,item,array)=>({title: item["Title"],rating:aitem["..."] }));
console.log(rating); 

用filter和map()对数组字符串进行筛选

// the global variable
var watchList = [{  "Title": "Inception","Year": "2010","Rated": "PG-13","Released": "16 Jul 2010","Runtime": "148 min","Genre": "Action, Adventure, Crime","Director": "Christopher Nolan","Writer": "Christopher Nolan","Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy","Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.","Language": "English, Japanese, French","Country": "USA, UK","Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.","Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg","Metascore": "74","imdbRating": "8.8","imdbVotes": "1,446,708","imdbID": "tt1375666","Type": "movie","Response": "True"},{  "Title": "Interstellar","Year": "2014","Rated": "PG-13","Released": "07 Nov 2014","Runtime": "169 min","Genre": "Adventure, Drama, Sci-Fi","Director": "Christopher Nolan","Writer": "Jonathan Nolan, Christopher Nolan","Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow","Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.","Language": "English","Country": "USA, UK","Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.","Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg","Metascore": "74","imdbRating": "8.6","imdbVotes": "910,366","imdbID": "tt0816692","Type": "movie","Response": "True"},{"Title": "The Dark Knight","Year": "2008","Rated": "PG-13","Released": "18 Jul 2008","Runtime": "152 min","Genre": "Action, Adventure, Crime","Director": "Christopher Nolan","Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)","Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine","Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.","Language": "English, Mandarin","Country": "USA, UK","Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.","Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg","Metascore": "82","imdbRating": "9.0","imdbVotes": "1,652,832","imdbID": "tt0468569","Type": "movie","Response": "True"},{  "Title": "Batman Begins","Year": "2005","Rated": "PG-13","Released": "15 Jun 2005","Runtime": "140 min","Genre": "Action, Adventure","Director": "Christopher Nolan","Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)","Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes","Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.","Language": "English, Urdu, Mandarin","Country": "USA, UK","Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.","Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg","Metascore": "70","imdbRating": "8.3","imdbVotes": "972,584","imdbID": "tt0372784","Type": "movie","Response": "True"},{"Title": "Avatar","Year": "2009","Rated": "PG-13","Released": "18 Dec 2009","Runtime": "162 min","Genre": "Action, Adventure, Fantasy","Director": "James Cameron","Writer": "James Cameron","Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang","Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.","Language": "English, Spanish","Country": "USA, UK","Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.","Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg","Metascore": "83","imdbRating": "7.9","imdbVotes": "876,575","imdbID": "tt0499549","Type": "movie","Response": "True"}
];// Add your code below this linevar filteredList=[];filteredList=watchList.map(function(elemtent){//先通过map遍历,删选所有title和ratingreturn {title:elemtent["Title"],rating:elemtent["imdbRating"]}
}).filter(x => x.rating >= 8);
// Add your code above this line
console.log(filteredList); 

forEach

function logArrayElement(element,index,arrary){console.log("a["+index+"]="+element);
}
[1,2,3,,4,""].forEach(logArrayElement);

让多个数组共享一个方法

var s = [23, 65, 98, 5];
var b=[1,3,5,7,8]
Array.prototype.myFilter = function(callback){var newArray = [];// Add your code below this linethis.forEach(function(item){
if(callback(item)){newArray.push(item);
}});// Add your code above this linereturn newArray;
};var new_s = s.myFilter(function(item){return item % 2 === 1;
});
var new_b = b.myFilter(function(item){
//console.log(item);return item;
})

Functional Programming相关推荐

  1. Clojure入门教程: Clojure – Functional Programming for the JVM中文版

    http://xumingming.sinaapp.com/302/clojure-functional-programming-for-the-jvm-clojure-tutorial/ api:h ...

  2. Functional Programming 资料收集

    书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...

  3. 啥叫“Functional Programming ”???

    啥叫"Functional Programming "??? 我们华师大软件工程专业开始上课了,用的全是英文版教材. 我们有一门课叫做 Functional Programming ...

  4. RxJS 系列之一 - Functional Programming 简介

    RxJS 系列目录 RxJS 系列之一 - Functional Programming 简介 (本文) RxJS 系列之二 - Observable 详解 RxJS 系列之三 - Operators ...

  5. Clojure – Functional Programming for the JVM中文版

    Clojure – Functional Programming for the JVM中文版 发表于 2011 年 12 月 07 日 由xumingming 作者: xumingming | 可以 ...

  6. [翻译]Why Functional Programming Matters

    Why Functional Programming Matters 函数式程序设计为什么至关重要 作者: John Hughes 翻译: CloudiDust [http://blog.csdn.n ...

  7. 重温“卡马克谈functional programming in c++”

    正在准备些关于overwatch的gameplay architecture的blog,里面的观点也让我想起12年时候看的carmack谈的functional programming in c++这 ...

  8. Kotlin 函数式编程(Kotlin Functional Programming)

    Kotlin 函数式编程 (Kotlin Functional Programming)     陈光剑 1. 函数式概述 6 1.1. 函数式简史 6 1.2. 函数式编程语言家族 7 1.2.1. ...

  9. Functional Programming in C++

    Probably everyone reading this has heard "functional programming" put forth as something t ...

  10. Functional Programming in Java venkat(4) Using Collections part2

    文章目录 Functional Programming in Java venkat(3): Using Collections part2 Introduction Using Collection ...

最新文章

  1. 深入Ajax架构和最佳实践读书笔记
  2. 关于VC中的stdafx.h
  3. c++经典书籍--Effective C++
  4. 判断任意控制台输入的十进制数是否为水仙花数
  5. mini2440的时钟
  6. python字符串长度_如何使用python获取字符串长度?哪些方法?
  7. 计算机辅助翻译课怎么学的,计算机辅助翻译原理与实践
  8. 保存 Hexo 博客源码到 GitHub
  9. 检测相关问题面试准备
  10. c语言写莫迪康通信,组态王modbus通信用法教程modbus-rtu、modbus-tcp莫迪康通信配置步骤...
  11. 各种说明方法的答题格式_说明方法作用答题格式
  12. MC34063在扩展后的降压应用
  13. 京东大数据技术白皮书(附下载)
  14. js判断两个时间是否超过一年
  15. 抖音提示原创度低,这会导致封号么?给你几点建议
  16. 《计算机网络管理》_Chap2
  17. 热泵烤房监测上云方案
  18. UIQ 3 概念认识
  19. G2 定制仪表盘实践
  20. chi square-卡方分布的定义及性质

热门文章

  1. 受贿千万,字节前餐饮主管二审被判6年
  2. 四大会计事务所薪酬待遇及发展各个方面大比拼
  3. 向量的内积(点积)、叉积(向量积)
  4. 【Ubuntu】Ubuntu18.04主题,图标,登陆美化
  5. Metasploit扫描漏洞模块
  6. softice调试驱动
  7. java heartbeat
  8. 以读者角度走进RFID打造的智慧图书馆
  9. 计算机进化阶段,进化计算
  10. 微软商店的iTunes备份文件路径正确修改方法(2022.3.25)