前言

【笔记内容】

【笔记目的】

【相关资源】

温馨提示

Array对象快速了解

PS什么是数组?

【笔记】【JavaScript】JSchallenger-Arrays对象-练习笔记

常见操作

方法 描述
let arr=[数组元素1,...,数组元素n] 创建数组
let el=arr[索引] 通过索引访问数组元素
arr.forEach(function(item,index,array)){} 遍历数组
arr.push(添加元素) 添加元素到数组的末尾
arr.pop() 删除数组末尾的元素
arr.shift() 删除数组头部元素
arr.push(目标元素) arr.indexOf(目标元素)
arr.splice(,) 通过索引删除某个元素
let shallowCopy = arr.slice() 复制一个数组

了解更多

JSchallenger-Arrays

Get nth element of array

需求:

Write a function that takes an array (a) and a value (n) as argument

Return the nth element of 'a'

我的提交(作者答案)

function myFunction(a, n) {
   return a[n - 1];
}

涉及知识(访问数组元素)

访问数组元素

格式

arr[index]

index:访问数组元素目标索引

了解更多

Remove first n elements of an array

需求:

Write a function that takes an array (a) as argument

Remove the first 3 elements of 'a'

Return the result

我的提交(作者答案)

function myFunction(a) {
   return a.slice(3);
}

涉及知识(slice()方法)

Array.prototype.slice()

格式

arr.slice([begin[, end]])

begin(提取起始处的索引):可选

end(提取终止处的索引):可选

返回值:一个含有被提取元素的新数组

了解更多

Get last n elements of an array

需求:

Write a function that takes an array (a) as argument

Extract the last 3 elements of a

Return the resulting array

我的提交(作者答案)

function myFunction(a) {
    return a.slice(-3);
}

涉及知识(slice()方法)

Array.prototype.slice()

点击此处跳转

Get first n elements of an array

需求:

Write a function that takes an array (a) as argument

Extract the first 3 elements of a

Return the resulting array

我的提交(作者答案)

function myFunction(a) {
   return a.slice(0, 3);
}

涉及知识(slice()方法)

Array.prototype.slice()

点击此处跳转

Return last n array elements

需求:

Write a function that takes an array (a) and a number (n) as arguments

It should return the last n elements of a

我的提交(作者答案)

function myFunction(a, n) {
  return a.slice(-n);
}

涉及知识(slice()方法)

Array.prototype.slice()

点击此处跳转

Remove a specific array element

需求:

Write a function that takes an array (a) and a value (b) as argument

The function should clean a from all occurrences of b

Return the filtered array

我的提交(作者答案)

function myFunction(a, b) {
   return a.filter(item => item !== b);
}

涉及知识(filter()方法,箭头函数)

array.filter()方法

格式(注意该格式不完整,之针对本题的格式

array.filter(function(currentValue))

function(currentValue):必需,函数,数组中的每个元素都会执行这个函数

返回值:数组。

了解更多

箭头函数

格式

(param1, param2, …, paramN) => expression
(param1, param2, …, paramN) => { statements }
//相当于:(param1, param2, …, paramN) =>{ return expression; }

param:参数

expression:表达式

其他格式 前提
singleParam => { statements } 只有一个参数时,圆括号是可选的:
() => { statements } 没有参数的函数应该写成一对圆括号。

了解更多

Count number of elements in JavaScript array

需求:

Write a function that takes an array (a) as argument

Return the number of elements in a

我的提交(作者答案)

function myFunction(a) {
   return a.length;
}

涉及知识(array.length)

Array.length

了解更多

Count number of negative values in array

需求:

Write a function that takes an array of numbers as argument

Return the number of negative values in the array

我的提交

function myFunction(a){
	var count=0;
	for(var i=0;i<a.length;i++){
   		if(a[i]<0){
      	count++;
   		}
	}
	return count;
}

作者答案

function myFunction(a) {
   return a.filter((el) => el < 0).length;
}

涉及知识(array.filter()方法、箭头函数)

array.filter()方法

点击此处跳转

箭头函数

点击此处跳转

Sort an array of numbers in descending order

需求:

Write a function that takes an array of numbers as argument

It should return an array with the numbers sorted in descending order

我的提交

function myFunction(arr) {
   arr1=arr.sort();
   return arr1.reverse();
}

作者答案

function myFunction( arr ) {
  return arr.sort((a, b) => b - a)
}

涉及知识(array.sort()方法、array.reverse()方法、箭头函数)

Array.prototype.sort()

格式(注意该格式不完整,之针对本题的格式

arr.sort([compareFunction])

compareFunction(指定按某种顺序进行排列的函数):可选。

返回值:数组

了解更多

Array.prototype.reverse()

格式

 arr.reverse()

返回值:颠倒后的数组。

箭头函数

点击此处跳转

Sort an array of strings alphabetically

需求:

Write a function that takes an array of strings as argument

Sort the array elements alphabetically

Return the result

我的提交(作者答案)

function myFunction(arr) {
   return arr.sort();
}

涉及知识(字母排序方法)

字母排序方法

使用Array.prototype.sort()方法排序

点击此处跳转

Return the average of an array of numbers

需求:

Write a function that takes an array of numbers as argument

It should return the average of the numbers

我的提交

function myFunction(arr){
	var sum=0;
	for(var i=0;i<arr.length;i++){
   		sum+=arr[i];
	}
	return sum/arr.length;
}

作者答案

function myFunction( arr ) {
	return arr.reduce((acc, cur) => acc + cur, 0) / arr.length
}

涉及知识(reduce()方法、箭头函数)

Array.prototype.reduce()

格式(注意该格式不完整,之针对本题的格式

array.reduce(function(total, currentValue))

function(total,currentValue)(用于执行每个数组元素的函数):必需。

返回值:计算结果

了解更多

箭头函数

点击此处跳转

Return the longest string from an array of strings

需求:

Write a function that takes an array of strings as argument、

Return the longest string

我的提交

function myFunction(arr) {
   var max;
   for(var i=0;iarr[i+1].length){
         max=arr[i];
      }else{
         max=arr[i+1];
      }
   }
   return max;
}

作者答案

function myFunction( arr ) {
	return arr.reduce((a, b) => a.length <= b.length ? b : a)
}

涉及知识(reduce()方法、箭头函数、三目运算符)

Array.prototype.reduce()

点击此处跳转

箭头函数

点击此处跳转

三目运算符

格式

expression ? sentence1 : sentence2

Check if all array elements are equal

需求:

Write a function that takes an array as argument

It should return true if all elements in the array are equal

It should return false otherwise

我的提交

function myFunction(arr) {
   return arr.every(item=>item ===arr[0]);
}

作者答案

function myFunction( arr ) {
  return new Set(arr).size === 1
}

涉及知识(every()方法、set().size方法、相等操作符)

Array.prototype.every()

注意:

格式(注意该格式不完整,之针对本题的格式

array.every(function(currentValue))

function(currentValue)(函数):必须。

返回值:布尔值

了解更多

set().size方法

相等操作符

相等和不等
相等 不等
== !=
相等返回true 不等返回true

全等和不全等
全等 不全等
=== !==

全等和不全等相等和不等的区别:

===!== ==!=
比较前操作数是否需要转换 不需要 需要

示例

var result1 ={"55" == 55 };		//true
var result2 ={"55" === 55 };	//false
var result3 ={"55" != 55 };		//false
var result4 ={"55" !== 55 };	//true
var result5 ={null == undefined };	//true
var result6 ={null === undefined };	//false

为了保持代码中数据类型的完整性,推荐使用全等和不全等操作符

Merge an arbitrary number of arrays

需求:

Write a function that takes arguments an arbitrary number of arrays

It should return an array containing the values of all arrays

我的提交

function myFunction(...arrays) {
   return [].concat(...arrays);
}

作者答案

function myFunction( ...arrays ) {
return arrays.flat()
}

涉及知识(concat()方法、flat()方法、扩展运算符、拼接数组的思路)

concat()方法

格式

array1.concat(array2, array3, ..., arrayX)

array2, array3, ..., arrayX:必需。要连接的数组。

返回值:已连接的数组

flat()方法

格式

var newArray = arr.flat([depth])

depth:指定要提取嵌套数组的结构深度,默认值为 1

返回值:一个包含将数组与子数组中所有元素的新数组

扩展运算符

了解更多

拼接数组思路(拓展)

思路一:concat方法

具体操作结合我的提交与涉及知识

思路二:flat()方法

具体操作结合作者答案与涉及知识

思路三:apply(推荐)

arr1.push.apply(arr1,arr2);

思路四:es6的写法(推荐)

arr1.push(...arrays);

Sort array by object property

需求:

Write a function that takes an array of objects as argument

Sort the array by property b in ascending order

Return the sorted array

我的提交

function myFunction(arr) {
    return arr.sort((a,b)=>a.b-b.b);
}

作者答案

function myFunction(arr) {
   const sort = (x, y) => x.b - y.b;
   return arr.sort(sort);
}

涉及知识(const、sort()方法、箭头函数)

const

格式

const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];

nameN:常量名称,可以是任意合法的标识符。

valueN:常量值,可以是任意合法的表达式。

了解更多

sort()方法

点击此处跳转

箭头函数

点击此处跳转

Merge two arrays with duplicate values

需求:

Write a function that takes two arrays as arguments

Merge both arrays and remove duplicate values

Sort the merge result in ascending order

Return the resulting array

 Write a function that takes two arrays as arguments
// Merge both arrays and remove duplicate values
// Sort the merge result in ascending order
// Return the resulting array

这道题我试了很多方法,都是说有一个或多个测试案例不过。请小伙伴们在留言区分享这道题的解法,谢谢啦!!!!

Sum up all array elements with values greater than

需求:

Write a function that takes an array (a) and a number (b) as arguments

Sum up all array elements with a value greater than b

Return the sum

我的提交

function myFunction(a, b) {
   var sum=0;
   for(var i=0;ib){
         sum+=a[i];
      }
   }
   return sum;
}

作者答案

function myFunction(a, b) {
  return a.reduce((sum, cur) => {
    if (cur > b) return sum + cur;
    return sum;
  }, 0);
}

涉及知识(reduce()方法,箭头函数)

Array.prototype.reduce()

点击此处跳转

箭头函数

点击此处跳转

Create a range of numbers

需求:

Write a function that takes two numbers (min and max) as arguments

Return an array of numbers in the range min to max

我的提交

function myFunction(min, max) {
   var arr = [];
   var length=max-min;
   var t=min;
   for(var i=0;i<=length;i++){
      arr[i]=t;
      t++;
   }
   return arr;
}

作者答案

function myFunction(min, max) {
  let arr = [];
  for (let i = min; i <= max; i++) {
    arr.push(i);
  }
return arr;
}

涉及知识(let、push()方法)

let

了解更多

push()方法

注意:

格式

 array.push(item1, item2, ..., itemX)

item1, item2, ..., itemX:必需。要添加到数组的元素。

返回值:数组

Group array of strings by first letter

需求

Write a function that takes an array of strings as argument

Group those strings by their first letter

Return an object that contains properties with keys representing first letters

The values should be arrays of strings containing only the corresponding strings

For example, the array ['Alf', 'Alice', 'Ben'] should be transformed to

{ a: ['Alf', 'Alice'], b: ['Ben']}

我的提交

function myFunction(arr){
    let resultObj = {};
  	for (let i =0; i < arr.length; i++) {
    	let currentWord = arr[i];
    	let firstChar = currentWord[0].toLowerCase();
    	let innerArr = [];
    	if (resultObj[firstChar] === undefined) {
       		innerArr.push(currentWord);
      		resultObj[firstChar] = innerArr
    	}else {
      		resultObj[firstChar].push(currentWord)
    	}
  	}
	return resultObj;
}

作者答案

function myFunction(arr) {
 return arr.reduce((acc, cur) => {
   const firstLetter = cur.toLowerCase().charAt(0);
   return { ...acc, [firstLetter]: [...(acc[firstLetter] || []), cur] };
 }, {});
}

涉及知识(toLowerCase()方法、相等操作符、push()方法、reduce()方法、charAt()方法、扩展运算符)

toLowerCase()

格式

stringObject.toLowerCase()

返回值:新的字符串,所有大写字符全部被转换为了小写字符

相等操作符

点击此处跳转

push()方法

点击此处跳转

reduce()方法

点击此处跳转

charAt()方法

格式

stringObject.charAt(index)

stringObject:字符串对象

index(字符所在的字符串中的下标):必需

扩展运算符

点击此处跳转

Define an array with conditional elements

需求:

Write a function that takes an array with arbitrary elements and a number as arguments

Return a new array, the first element should be either the given number itself

or zero if the number is smaller than 6

The other elements should be the elements of the original array

Try not to mutate the original array

我的提交

function myFunction(arr, num) {
   return num>=6? [num].concat(arr):[0].concat(arr);
}

作者答案

function myFunction(arr, num) {
 return [...(num > 5 ? [num] : [0]), ...arr];
}

涉及知识(不改变原数组的方法、扩展运算符、三目运算符)

不改变原数组的方法

不改变原数组的方法 描述
concat 返回拼接后的数组,不改变原数组;
forEach 遍历数组
map
join() 返回拼接后的字符串,可以指定间隔;
slice(start,end); 截取数组,返回截取的部分,不改变原始数组;
sort(); 排序
toString(); [1,2,3].toString()==[1,2,3].join();

扩展运算符

点击此处跳转

三目运算符

点击此处跳转

Get every nth element of array

需求:

Write a function that takes an array (a) and a value (n) as arguments

Save every nth element in a new array

Return the new array

我的提交

function myFunction(a, n){
	var arr=[];
	for(var i=0;i<a.length;i++){
   		if((i+1)%n==0){
      	arr.push(a[i]);
   		}
	}
	return arr;
}

作者答案

function myFunction(a, n) {
   let rest = [...a];
   let result = [];
   for (let i = 0; i < a.length; i++) {
      if (rest.length < n) break;
      result.push(rest[n - 1]);
      rest = rest.slice(n);
   }
   return result;
}

涉及知识(扩展运算符、push()方法、slice()方法)

扩展运算符

点击此处跳转

push()方法

点击此处跳转

slice()方法

点击此处跳转

结语

【创作背景】

​ 偶然在抖音上刷到JSchallenger这个可以训练Javascript基础的网站,自己在完成所有的Schallenger题之后,想要通过博客来记录自己的做题痕迹,以便日后快速回顾。原本打算把JSchallenger的所有题目以及分析整理成一篇博客发出来,但是我整理完后发现,已经快有1w多字,为了方便读者和自己观看,因此打算按照JSchallenger的板块分开发布。

【感谢】

感谢各位读者能看到最后!!!