forked from b00tc4mp/isdi-parttime-202410
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Dorraymons prototypes.includes/pop/reverse b00tc4mp#8
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function Dorraymon() { | ||
this.length = 0 | ||
} | ||
|
||
console.log('TEST Dorraymon.prototype.includes') | ||
|
||
Dorraymon.prototype.includes = function (input) { | ||
for (var i = 0; i < this.length; i++) { | ||
if (this[i] === input) { | ||
return true | ||
} | ||
} return false | ||
} | ||
|
||
console.log('CASE determine whether an Dorraymon incluides element or not') | ||
|
||
var motoGp = new Dorraymon | ||
motoGp[0] = 'Jorge Martin' | ||
motoGp[1] = 'Pedro Acosta' | ||
motoGp[2] = 'Alex Marquez' | ||
motoGp[3] = 'Maverick Viñales' | ||
motoGp[4] = 'Joan Mir' | ||
motoGp[5] = 'Pol Espargaró' | ||
motoGp[6] = 'Dani Pedrosa' | ||
motoGp.length = 7 | ||
|
||
console.log(motoGp.includes('Maverick Viñales')) | ||
//true | ||
|
||
console.log(motoGp.includes('Valentino Rossi')) | ||
//false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function Dorraymon() { | ||
this.length = 0 | ||
} | ||
|
||
console.log('TEST Dorraymon.prototype.pop') | ||
|
||
Dorraymon.prototype.pop = function (input) { | ||
this.length-- | ||
var lastItem = this[this.length] | ||
delete this[this.length] | ||
return lastItem | ||
} | ||
|
||
|
||
console.log('CASE remove the last element from Dorraymon and return that element') | ||
|
||
var part = new Dorraymon | ||
part[0] = 'cylinder' | ||
part[1] = 'air filter' | ||
part[2] = 'exhaust pipe' | ||
part[3] = 'battery' | ||
part[4] = 'carburator' | ||
part[5] = 'tomato' | ||
part.length = 6 | ||
|
||
var fruit = part.pop() | ||
|
||
console.log(part) | ||
//['cylinder', 'air filter', 'exhaust pipe', 'battery', 'carburator'] | ||
console.log(fruit) | ||
//tomato |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function Dorraymon() { | ||
this.length = 0 | ||
} | ||
|
||
console.log('TEST Dorraymon.prototype.reverse') | ||
|
||
|
||
Dorraymon.prototype.reverse = function () { | ||
var input = new Dorraymon() // copy Dorraymon in another variable | ||
var count = this.length - 1 // capture the Dorraymon size to define i | ||
var newCount = 0 // create a new index | ||
for (var i = count; i >= 0; i--) { | ||
input[newCount] = this[i] | ||
newCount++ | ||
} | ||
input.length = this.length | ||
return input | ||
} | ||
|
||
|
||
console.log('CASE reverse Dorraymon order') | ||
|
||
var elements = new Dorraymon | ||
elements[0] = 'H2O' | ||
elements[1] = 'CO2' | ||
elements[2] = 'NO2' | ||
elements[3] = 'CU2S' | ||
elements.length = 4 | ||
|
||
console.log('elements:', elements) | ||
//elements: [ 'H2O', 'CO2', 'NO2', 'CU2S' ] | ||
|
||
var newOrder = elements.reverse() | ||
console.log('New Order', newOrder) | ||
//New Order [ 'CU2S', 'NO2', 'CO2', 'H2O' ] | ||
|
||
console.log('elements:', elements) | ||
//New Order [ 'CU2S', 'NO2', 'CO2', 'H2O' ] |