1、字符串

1.sum two numbers

  1. function myFunction(a, b)
  2. {
  3. return a+b
  4. }

2.Comparison operators, strict equality

  1. function myFunction(a, b)
  2. {
  3. return a === b
  4. }

3.Get type of value

  1. function myFunction(a)
  2. {
  3. return typeof(a)
  4. }

4.Get nth character of string

  1. function myFunction(a, n) {
  2. return a[n-1]
  3. }

5.Remove first n characters of string

  1. function myFunction(a) {
  2. return a.slice(3)
  3. }

6.Get last n characters of string

  1. function myFunction(str) {
  2. return str.slice(str.length-3)
  3. }

7.Get first n characters of string

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

8.Extract first half of string

  1. function myFunction(a) {
  2. return a.slice(0, a.length / 2)
  3. }

9.Remove last n characters of string

  1. function myFunction(a) {
  2. return a.slice(0,a.length-3)
  3. }

10.Check if a number is even

  1. function myFunction(a) {
  2. return a % 2 === 0
  3. }

2、数组

1.Get nth element of array

  1. function myFunction(a, n) {
  2. return a[n-1]
  3. }

2.Remove first n elements of an array

  1. function myFunction(a) {
  2. return a.slice(3)
  3. }

3.Get last n elements of an array

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

4.Get first n elements of an array

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

5.Remove a specific array element

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

6.Count number of elements in JavaScript array

  1. function myFunction(a) {
  2. return a.length
  3. }

7.Sort an array of strings alphabetically

  1. function myFunction(arr) {
  2. return arr.sort()
  3. }

8.Sort an array of numbers in descending order

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

9.Calculate the sum of an array of numbers

  1. function myFunction(a) {
  2. let sum = 0
  3. for(let i = 0;i<a.length;i++){
  4. sum += a[i]
  5. }
  6. return sum
  7. }

3、对象

1.Accessing object properties one

  1. function myFunction(obj) {
  2. return obj["country"];
  3. }

2.Accessing object properties three

  1. function myFunction(obj, key) {
  2. return obj[key];
  3. }

3.Check if property exists in object

  1. function myFunction(a, b) {
  2. return b in a;
  3. }

4.Check if property exists in object and is truthy
5.Creating Javascript objects one

  1. function myFunction(a) {
  2. return { key: a };
  3. }

6.Creating Javascript objects two

  1. function myFunction(a, b) {
  2. return {[a]:b}
  3. }

7.Creating Javascript objects three

  1. function myFunction(a, b) {
  2. let obj = {}
  3. for(let i = 0; i < a.length; i++){
  4. obj[a[i]] = b[i]
  5. }
  6. return obj
  7. }

8.Extract keys from Javascript object

  1. function myFunction(a) {
  2. return Object.keys(a)
  3. }

9.Sum object values

  1. function myFunction(a) {
  2. return eval(Object.values(a).join("+"))
  3. }

10.Remove a property from an object

  1. function myFunction(obj) {
  2. let {b,...item} = obj
  3. return item
  4. }