Skip to content

Commit

Permalink
arrays/numbers/strings
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-sm committed Aug 12, 2018
1 parent 725e1d1 commit ea6e59b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
File renamed without changes.
11 changes: 11 additions & 0 deletions javascript/array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const array = require('./array')

test('sum array values', () => {
expect(array.sum([1,2,3])).toBe(6)
expect(array.sum([])).toBe(0)
})

test('sum object array', () => {
expect(array.sumObj([{x:1}, {y:2}, {z:3}])).toBe(6)
expect(array.sumObj([])).toBe(0)
})
9 changes: 9 additions & 0 deletions javascript/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports.base7 = num => {
result = ''
while (num > 0) {
result = `${num % 7}${result}`
num = Math.floor(num / 7)
}

return parseInt(result)
}
7 changes: 7 additions & 0 deletions javascript/number.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const number = require('./number')

test('convert number to base7', () => {
expect(number.base7(384605)).toBe(3161204)
expect(number.base7(7)).toBe(10)
expect(number.base7(6)).toBe(6)
})
11 changes: 0 additions & 11 deletions javascript/reduce.test.js

This file was deleted.

14 changes: 14 additions & 0 deletions javascript/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports.indexOf = (str, word) => {
for (let i = 0; i < str.length; i++) {
for (let k = 0; k < word.length; k ++) {
if (str[i + k] !== word[k]) {
break
}

if (k === word.length - 1) {
return i
}
}
}
return -1
}
8 changes: 8 additions & 0 deletions javascript/string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const string = require('./string')

test('indexOf', () => {
expect(string.indexOf('abcdef', 'cde')).toBe(2)
expect(string.indexOf('abcdef', 'zzz')).toBe(-1)
expect(string.indexOf('abcdef', 'f')).toBe(5)
expect(string.indexOf('aaaaa', 'a')).toBe(0)
})

0 comments on commit ea6e59b

Please sign in to comment.