Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // Array.prototype.map()
- /*
- The map() method creates a new array with the results of calling a
- provided function on every element in this array.
- Return value:
- A new array with each element being the result of the callback function.
- */
- var numbers = [1, 2, 3];
- var roots = numbers.map(function(x){
- return x * x * x;
- });
- // roots is now [1, 8, 27]
- // numbers is still [1, 2, 3]
- function map(array, callbackFunction) {
- var newArray = [];
- array.forEach(function(element) {
- newArray.push(callbackFunction(element));
- });
- return newArray;
- }
- function cubeAll(numbers) {
- return map(numbers, function(n) {
- return n * n * n;
- });
- }
- function cube(n) {
- return n * n * n;
- }
- console.log(cubeAll([1, 2, 3]));
- console.log(map([1, 2, 3], cube));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// Array.prototype.map()
- /*
- The map() method creates a new array with the results of calling a
- provided function on every element in this array.
- Return value:
- A new array with each element being the result of the callback function.
- */
- var numbers = [1, 2, 3];
- var roots = numbers.map(function(x){
- return x * x * x;
- });
- // roots is now [1, 8, 27]
- // numbers is still [1, 2, 3]
- function map(array, callbackFunction) {
- var newArray = [];
- array.forEach(function(element) {
- newArray.push(callbackFunction(element));
- });
- return newArray;
- }
- function cubeAll(numbers) {
- return map(numbers, function(n) {
- return n * n * n;
- });
- }
- function cube(n) {
- return n * n * n;
- }
- console.log(cubeAll([1, 2, 3]));
- console.log(map([1, 2, 3], cube));</script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement