Skip to content

Commit

Permalink
add find test and implementation b00tc4mp#160
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbarzi committed Oct 7, 2024
1 parent c0ae312 commit 97d02c6
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
51 changes: 51 additions & 0 deletions staff/manuel-barzi/playground/arrays/Array.prototype.find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
console.log('TEST Array.prototype.find')

console.log('CASE find first greater than 10')

var nums = [5, 12, 8, 130, 44]
var found = nums.find(function (num) { return num > 10 });
console.log(found);
// 12

console.log('CASE find first greater than 100')

var nums = [5, 12, 8, 130, 44]
var found = nums.find(function (num) { return num > 100 });
console.log(found);
// 130

console.log('CASE find first name with length lower than 3')

var names = ['Peter', 'Leo', 'Wendy', 'Io', 'Mark']
var found = names.find(function (name) { return name.length < 3 })
console.log(found)
// Io

console.log('CASE find product in cart with more than 5 items')

var cart = [
{ brand: 'Nike', model: 'Air Max', quantity: 2 },
{ brand: 'Puma', model: 'Gatopardo', quantity: 1 },
{ brand: 'Adidas', model: 'Black', quantity: 7 },
{ brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }
]
var product = cart.find(function (product) {
//return product['quantity'] > 5
return product.quantity > 5
})
console.log(product)
// { brand: 'Adidas', model: 'Black', quantity: 7 }

console.log('CASE find product in cart with brand containing z')

var cart = [
{ brand: 'Nike', model: 'Air Max', quantity: 2 },
{ brand: 'Puma', model: 'Gatopardo', quantity: 1 },
{ brand: 'Adidas', model: 'Black', quantity: 7 },
{ brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }
]
var product = cart.find(function (product) {
return product.brand.includes('z')
})
console.log(product)
// { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }
65 changes: 65 additions & 0 deletions staff/manuel-barzi/playground/iterables/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var find = function (iterable, callback) {
for (var i = 0; i < iterable.length; i++) {
var element = iterable[i]

var found = callback(element)

if (found) return element
}

//return undefined
}

console.log('TEST find')

console.log('CASE find first greater than 10')

var nums = { '0': 5, '1': 12, '2': 8, '3': 130, '4': 44, 'length': 5 }
var found = find(nums, function (num) { return num > 10 });
console.log(found);
// 12

console.log('CASE find first greater than 100')

var nums = { '0': 5, '1': 12, '2': 8, '3': 130, '4': 44, 'length': 5 }
var found = find(nums, function (num) { return num > 100 });
console.log(found);
// 130

console.log('CASE find first name with length lower than 3')

var names = { 0: 'Peter', 1: 'Leo', 2: 'Wendy', 3: 'Io', 4: 'Mark', length: 5 }
var found = find(names, function (name) { return name.length < 3 })
console.log(found)
// Io

console.log('CASE find product in cart with more than 5 items')

var cart = {
0: { brand: 'Nike', model: 'Air Max', quantity: 2 },
1: { brand: 'Puma', model: 'Gatopardo', quantity: 1 },
2: { brand: 'Adidas', model: 'Black', quantity: 7 },
3: { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 },
length: 4
}
var product = find(cart, function (product) {
//return product['quantity'] > 5
return product.quantity > 5
})
console.log(product)
// { brand: 'Adidas', model: 'Black', quantity: 7 }

console.log('CASE find product in cart with brand containing z')

var cart = {
0: { brand: 'Nike', model: 'Air Max', quantity: 2 },
1: { brand: 'Puma', model: 'Gatopardo', quantity: 1 },
2: { brand: 'Adidas', model: 'Black', quantity: 7 },
3: { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 },
length: 4
}
var product = find(cart, function (product) {
return product.brand.includes('z')
})
console.log(product)
// { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }
83 changes: 83 additions & 0 deletions staff/manuel-barzi/playground/raids/Raid.prototype.find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var Raid = function () { this.length = 0 }

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

var found = callback(element)

if (found) return element
}

//return undefined
}

console.log('TEST Raid.prototype.find')

console.log('CASE find first greater than 10')

var nums = new Raid
nums['0'] = 5
nums['1'] = 12
nums['2'] = 8
nums['3'] = 130
nums['4'] = 44
nums.length = 5
var found = nums.find(function (num) { return num > 10 });
console.log(found);
// 12

console.log('CASE find first greater than 100')

var nums = new Raid
nums['0'] = 5
nums['1'] = 12
nums['2'] = 8
nums['3'] = 130
nums['4'] = 44
nums.length = 5
var found = nums.find(function (num) { return num > 100 });
console.log(found);
// 130

console.log('CASE find first name with length lower than 3')

var names = new Raid
names['0'] = 'Peter'
names['1'] = 'Leo'
names['2'] = 'Wendy'
names['3'] = 'Io'
names['4'] = 'Mark'
names.length = 5
var found = names.find(function (name) { return name.length < 3 })
console.log(found)
// Io

console.log('CASE find product in cart with more than 5 items')

var cart = new Raid
cart[0] = { brand: 'Nike', model: 'Air Max', quantity: 2 }
cart[1] = { brand: 'Puma', model: 'Gatopardo', quantity: 1 }
cart[2] = { brand: 'Adidas', model: 'Black', quantity: 7 }
cart[3] = { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }
cart.length = 4
var product = cart.find(function (product) {
//return product['quantity'] > 5
return product.quantity > 5
})
console.log(product)
// { brand: 'Adidas', model: 'Black', quantity: 7 }

console.log('CASE find product in cart with brand containing z')

var cart = new Raid
cart[0] = { brand: 'Nike', model: 'Air Max', quantity: 2 }
cart[1] = { brand: 'Puma', model: 'Gatopardo', quantity: 1 }
cart[2] = { brand: 'Adidas', model: 'Black', quantity: 7 }
cart[3] = { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }
cart.length = 4
var product = cart.find(function (product) {
return product.brand.includes('z')
})
console.log(product)
// { brand: 'Santacruz', model: 'Longboard 123', quantity: 4 }

0 comments on commit 97d02c6

Please sign in to comment.