Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/carray #238

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions staff/david-aroca/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
Empty file.
62 changes: 62 additions & 0 deletions staff/david-aroca/Carray/Carray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function Carray() {
var elements = arguments

for (var i = 0; i < Carrayarray.length; i++) {
var element = element[i];

this[i] = element
}
this.length = elements.length
}

// Carray.prototype.array.forEach(element => {});

Carray.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) {
var element = this[i];

callback(element)
}
}

Carray.prototype.find = function (callback) {
for (var i = 0; i < this.length; i++) {
var element = this[i];

var matches = callback(element)

if (matches)
return element

}
}

Carray.prototype.map = function (callback) {
var results = new Carray

for (var i = 0; i < array.length; i++) {
const element = this[i];

var mappedElemet = callback(element)

results[results.length] = mappedElemet
results.length++

}

return results
}

Carray.prototype.find = Function(callback){
var results = new Carray()

for (var i = 0; i < this.length; i++) {
if (callback(this{ i })) {

}

}
return results
}

module.exports = Carray
113 changes: 113 additions & 0 deletions staff/david-aroca/Carray/Carray.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
var Carray = require('./Carray')

console.log('TEST Carray')

console.log('> constructor')

console.log('CASE construct an instance without element')

var c = new Carray

console.log(c)
// Carray (length: 0)

console.log('CASE constructor an instance with number elements')

var c = new Carray(10, 20, 30)

console.log(c)
//Carray { 0:10, 1: 20, 2: 30, length: 3}

console.log('CASE constructs an instance with string elements')

var c = new Carray('Ana', 'Adrian', 'Javier', 'Sergio', 'Bernar', 'Maite', 'Sara')

console.log(c)
// Carray {0: 'Ana', 1: 'Adrian, 'Javier', 'Sergio', 'Bernat', 'Maite', 'Sara', length 7

console.log('> forEach')

console.log('CASE iterates an instance with number elements')

var c = new Carray(10, 20, 30)

c.forEach(function (element) {
console.log(element)
})
// 10
// 20
// 30

console.log('CASE iterates an instance with string to upper-case')

var c = new Carray('Ana', 'Adrian', 'Javier', 'Sergio', 'Bernat', 'Maite', 'Sara')

c.forEach(function (element) {
console.log(element.toUpperCase())
})

// ANA
// ADRIAN
// JAVIER
// SERGIO
// BERNAT
// MAITE
// SARA

console.log('> find')

console.log('CASE find person with age 20')

var people = new Carray(
{ name: 'Peter', age: 30 },
{ name: 'Wendy', age: 25 },
{ name: 'James', age: 20 },
{ name: 'Campa', age: 15 }
)

var person = people.find(function (person) {
return person.age === 20
})

console.log(person)
// { name: 'James', age: 20}

console.log('> map')

console.log('CASE maps products from cart to subtotals')

var cart = new Carray(
{ name: 'Socks Adidas', price: 20, quantity: 2 },
{ name: 'Nike Air Max', price: 80, quantity: 1 },
{ name: 'Shorts Puma', price: 30, quantity: 4 },
)

var subtotals = cart.map(function () {
return product.price * product.quantity
})

console.log(subtotals)
//Carray { 0:40, 1:80, 2:90, 3:200, length: 4}

console.log('CASE maps names to upper-case')

var names = new Carray('Ana', 'Adrian', 'Javier', 'Sergio', 'Bernat', 'Maite', 'Sara')

var namesinUpperCase = names.map(function (name) {
return name.toUpperCase()
})

console.log(namesinUpperCase)
// Carray {0: 'ANA', 1: 'ADRIAN', 2: 'JAVIER', 3 'SERGIO', 4: 'BERNAT', 5: 'MAITE', 6: 'SARA', lentgh: 7}


console.log('CASE find the names whith (A)')

var names = new Carray('Ana', 'Adrian', 'Javier', 'Sergio', 'Bernat', 'Maite', 'Sara')

var nanesWithA = names.find(function(name){
return name.includes('a')
})

console.log(namesinUpperCase)