Skip to content

Commit

Permalink
add Dorraymons prototypes.includes/pop/reverse b00tc4mp#8
Browse files Browse the repository at this point in the history
  • Loading branch information
frameloop committed Nov 3, 2024
1 parent 9167dcd commit 549f320
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
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
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
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' ]

0 comments on commit 549f320

Please sign in to comment.