Skip to content

Commit

Permalink
add indexOf test and implementation; add test case for push with n ar…
Browse files Browse the repository at this point in the history
…guments; b00tc4mp#160
  • Loading branch information
manuelbarzi committed Sep 27, 2024
1 parent 19854ba commit dfa29f8
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 4 deletions.
17 changes: 17 additions & 0 deletions staff/manuel-barzi/playground/arrays/Array.prototype.indexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
console.log('TEST Array.prototype.indexOf')

console.log('CASE get index of c')

var chars = ['a', 'b', 'c', 'b', 'a']

var index = chars.indexOf('c')
console.log(index)
// 2

console.log('CASE get index of c from index -2')

var chars = ['a', 'b', 'c', 'b', 'a']

var index = chars.indexOf('c', -2)
console.log(index)
// -1
12 changes: 11 additions & 1 deletion staff/manuel-barzi/playground/arrays/Array.prototype.push.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ var length = fruits.push('banana')
console.log(fruits)
// ['apple', 'orange', 'raspberry', 'pineapple', 'banana'] (5)
console.log(length)
// 5
// 5

console.log('CASE add banana pear and coconut to fruits')

var fruits = ['apple', 'orange', 'raspberry', 'pineapple']
var length = fruits.push('banana', 'pear', 'coconut')

console.log(fruits)
// ['apple', 'orange', 'raspberry', 'pineapple', 'banana', 'pear', 'coconut'] (7)
console.log(length)
// 7
25 changes: 25 additions & 0 deletions staff/manuel-barzi/playground/iterables/indexOf.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var indexOf = function (iterable, searchElement) {
/*
iterate on iterable
if searchElement found then return index
otherwise return -1
*/

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

if (element === searchElement) return i
}

return -1
}

console.log('TEST indexOf')

console.log('CASE get index of c')

var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 }

var index = indexOf(chars, 'c')
console.log(index)
// 2
43 changes: 43 additions & 0 deletions staff/manuel-barzi/playground/iterables/indexOf.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var indexOf = function (iterable, searchElement, fromIndex) {
/*
iterate on iterable
if searchElement found then return index
otherwise return -1
*/

/*
iterate on iterable from index fromIndex
if searchElement found then return index
otherwise return -1
*/

for (
var i = (fromIndex === undefined ? 0 : (fromIndex >= 0 ? fromIndex : fromIndex + iterable.length));
i < iterable.length;
i++
) {
var element = iterable[i]

if (element === searchElement) return i
}

return -1
}

console.log('TEST indexOf')

console.log('CASE get index of c')

var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 }

var index = indexOf(chars, 'c')
console.log(index)
// 2

console.log('CASE get index of c from index -2')

var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 }

var index = indexOf(chars, 'c', -2)
console.log(index)
// -1
43 changes: 43 additions & 0 deletions staff/manuel-barzi/playground/iterables/indexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var indexOf = function (iterable, searchElement, fromIndex) {
/*
iterate on iterable
if searchElement found then return index
otherwise return -1
*/

/*
iterate on iterable from index fromIndex
if searchElement found then return index
otherwise return -1
*/

for (
var i = (arguments.length === 2 ? 0 : (fromIndex >= 0 ? fromIndex : fromIndex + iterable.length));
i < iterable.length;
i++
) {
var element = iterable[i]

if (element === searchElement) return i
}

return -1
}

console.log('TEST indexOf')

console.log('CASE get index of c')

var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 }

var index = indexOf(chars, 'c')
console.log(index)
// 2

console.log('CASE get index of c from index -2')

var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 }

var index = indexOf(chars, 'c', -2)
console.log(index)
// -1
28 changes: 28 additions & 0 deletions staff/manuel-barzi/playground/iterables/push.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var push = function (iterable, element) {
iterable[iterable.length] = element
iterable.length++

return iterable.length
}

console.log('TEST push')

console.log('CASE add 400 to nums')

var nums = { 0: 100, 1: 200, 2: 300, length: 3 }
var length = push(nums, 400)

console.log(nums)
// { 0: 100, 1: 200, 2: 300, 3: 400, length: 4 }
console.log(length)
// 4

console.log('CASE add banana to fruits')

var fruits = { 0: 'apple', 1: 'orange', 2: 'raspberry', 3: 'pineapple', length: 4 }
var length = push(fruits, 'banana')

console.log(fruits)
// { 0: 'apple', 1: 'orange', 2: 'raspberry', 3: 'pineapple', 4: 'banana', length: 5 }
console.log(length)
// 5
25 changes: 22 additions & 3 deletions staff/manuel-barzi/playground/iterables/push.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
var push = function (iterable, element) {
iterable[iterable.length] = element
iterable.length++
if (arguments.length === 2) {
iterable[iterable.length] = element
iterable.length++
} else {
for (var i = 1; i < arguments.length; i++) {
var element = arguments[i]

iterable[iterable.length] = element
iterable.length++
}
}

return iterable.length
}
Expand All @@ -25,4 +34,14 @@ var length = push(fruits, 'banana')
console.log(fruits)
// { 0: 'apple', 1: 'orange', 2: 'raspberry', 3: 'pineapple', 4: 'banana', length: 5 }
console.log(length)
// 5
// 5

console.log('CASE add banana pear and coconut to fruits')

var fruits = { 0: 'apple', 1: 'orange', 2: 'raspberry', 3: 'pineapple', length: 4 }
var length = push(fruits, 'banana', 'pear', 'coconut')

console.log(fruits)
// ['apple', 'orange', 'raspberry', 'pineapple', 'banana', 'pear', 'coconut'] (7)
console.log(length)
// 7

0 comments on commit dfa29f8

Please sign in to comment.