https://github.com/bell-kevin/training-js-4-basic-data-types--array
In javascript, Array is one of basic data types. Define an empty array can use var arr=new Array() or var arr=[] Array has an attribute: length, if there is an array named arr, using arr.length to know how many elements are contained in the array. Each element in the array has an index, use arr[index] to get the value of element. index always start from 0, so the first element of array is arr[0], the last element of array is arr[arr.length-1]. If we want to add new elements to the array, you can use the array method: push(). It can add an element to the end of the array. Instead, if we want to remove the last element of the array, you can use the array method: pop(). for example: var arr=[1,2,3]; //define an array arr contains elements 1 2 3 arr.push(4); //add element 4 to arr console.log(arr) //[1,2,3,4] arr.pop(); //remove the last element from arr console.log(arr) //[1,2,3] Task I've written five function, each function receives a parameter: arr(an array), you should code something with arr. 1. getLength(arr) should return length of arr 2. getFirst(arr) should return the first element of arr 3. getLast(arr) should return the last element of arr 4. pushElement(arr) should push an element to arr, and then return arr 5. popElement(arr) should pop an element from arr, and then return arr When you have finished the work, click "Run Tests" to see if your code is working properly. In the end, click "Submit" to submit your code pass this kata.
https://github.com/bell-kevin/training-js-4-basic-data-types--array
codewars-kata-solution
Last synced: over 1 year ago
JSON representation
In javascript, Array is one of basic data types. Define an empty array can use var arr=new Array() or var arr=[] Array has an attribute: length, if there is an array named arr, using arr.length to know how many elements are contained in the array. Each element in the array has an index, use arr[index] to get the value of element. index always start from 0, so the first element of array is arr[0], the last element of array is arr[arr.length-1]. If we want to add new elements to the array, you can use the array method: push(). It can add an element to the end of the array. Instead, if we want to remove the last element of the array, you can use the array method: pop(). for example: var arr=[1,2,3]; //define an array arr contains elements 1 2 3 arr.push(4); //add element 4 to arr console.log(arr) //[1,2,3,4] arr.pop(); //remove the last element from arr console.log(arr) //[1,2,3] Task I've written five function, each function receives a parameter: arr(an array), you should code something with arr. 1. getLength(arr) should return length of arr 2. getFirst(arr) should return the first element of arr 3. getLast(arr) should return the last element of arr 4. pushElement(arr) should push an element to arr, and then return arr 5. popElement(arr) should pop an element from arr, and then return arr When you have finished the work, click "Run Tests" to see if your code is working properly. In the end, click "Submit" to submit your code pass this kata.
- Host: GitHub
- URL: https://github.com/bell-kevin/training-js-4-basic-data-types--array
- Owner: bell-kevin
- License: gpl-3.0
- Created: 2022-06-29T01:13:00.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-06T22:34:31.000Z (almost 4 years ago)
- Last Synced: 2025-01-23T16:48:29.174Z (over 1 year ago)
- Topics: codewars-kata-solution
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0