Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>JS Functions Task 1</title>
- <link href="css/js-console.css" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <p>Write a function that removes all elements with a given value
- <br/>var arr = [1,2,1,4,1,3,4,1,111,3,2,1,"1"];
- <br/>arr.remove(1); //arr = [2,4,3,4,111,3,2,"1"];
- <br/>Attach it to the array class. Read about prototype and how to attach methods
- </p>
- <div id="js-console"></div>
- <script src="js/js-console.js"></script>
- <script>
- //Add dynamically to the already defined object a new function
- Array.prototype.remove = function (number) {
- for (var i = 0; i < this.length; i++) {
- if (this[i] === number)
- this.splice(i, 1); // Here we don't use delete, because delete will not update the length of the array
- // neither really erases the element, only replaces it with the special value undefined
- }
- };
- var arr = [1, 2, 1, 4, 1, 3,"1", 4, 1, 111, 3, 2, 1, "1"];
- jsConsole.writeLine("We're given this array: ");
- jsConsole.writeLine(arr.join(', '));
- arr.remove(1);
- jsConsole.writeLine("After remove the digit 1: ");
- jsConsole.writeLine(arr.join(', '));
- arr.remove("1");
- jsConsole.writeLine("After remove the string 1: ");
- jsConsole.writeLine(arr.join(', '));
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement