Skip to content

Commit 93b86c5

Browse files
committed
changing to rest parameters
1 parent b1a7111 commit 93b86c5

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

12-chapter-Sorting-Algorithms/basic-sort.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
}
5151

5252
const selectionObject = {
53-
sort (args) {
54-
swap = [].slice.call(arguments, 1)[0]
55-
alist = args
53+
sort (...args) {
54+
alist = args[0]
55+
swap = args[1]
5656
selectionSort(0, alist.length - 1)
5757
return alist
5858
}

12-chapter-Sorting-Algorithms/quick-sort.module.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
}
3232

3333
const quickObject = {
34-
sort (args) {
35-
swap = [].slice.call(arguments, 1)[0]
36-
alist = args
34+
sort (...args) {
35+
alist = args[0]
36+
swap = args[1]
3737
quickSortHelper(0, alist.length - 1)
3838
return alist
3939
}

13-chapter-Searching-Algorithms/search-module.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function (exports) {
2-
function sequentialSearch (dataStore, data) {
2+
const sequentialSearch = (dataStore, data) => {
33
for (var i = 0; i < dataStore.length; i++) {
44
if (dataStore[i] === data) {
55
return i
@@ -8,7 +8,7 @@
88
return -1
99
}
1010

11-
function binarySearch (dataStore, data) {
11+
const binarySearch = (dataStore, data) => {
1212
let upperBound = dataStore.length - 1
1313
let lowerBound = 0
1414
while (lowerBound <= upperBound) {
@@ -24,7 +24,7 @@
2424
return -1
2525
}
2626

27-
function countOccurrences (dataStore, data) {
27+
const countOccurrences = (dataStore, data) => {
2828
let count = 0
2929
let position = binarySearch(dataStore, data)
3030
if (position > -1) {
@@ -47,7 +47,7 @@
4747
return count
4848
}
4949

50-
function minValue (dataStore) {
50+
const minValue = (dataStore) => {
5151
let min = dataStore[0]
5252
for (var i = 1; i < dataStore.length; i++) {
5353
if (dataStore[i] < min) {
@@ -57,7 +57,7 @@
5757
return min
5858
}
5959

60-
function maxValue (dataStore) {
60+
const maxValue = (dataStore) => {
6161
let max = dataStore[0]
6262
for (var i = 1; i < dataStore.length; i++) {
6363
if (dataStore[i] > max) {
@@ -67,5 +67,5 @@
6767
return max
6868
}
6969

70-
Object.assign(exports, {sequentialSearch}, {binarySearch}, {minValue}, {maxValue}, {countOccurrences})
70+
Object.assign(exports, {sequentialSearch, binarySearch, minValue, maxValue, countOccurrences})
7171
}((typeof module.exports !== undefined) ? module.exports : window))

13-chapter-Searching-Algorithms/test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ const fs = require('fs')
44

55
const nsTime = (hrtime) => hrtime[0] * 1e9 + hrtime[1]
66

7-
let data = fs.readFileSync('./13-chapter-Searching-Algorithms/paragraph.txt', 'utf8')
8-
.trim()
9-
.split(' ')
10-
11-
function setData (numElements = 100) {
7+
const setData = (numElements = 100) => {
128
let dataStore = []
139
for (let i = 0; i < numElements; i++) {
1410
dataStore[i] = Math.floor(Math.random() * (numElements + 1))
@@ -30,17 +26,23 @@ test ('Binary and Sequential search on array of numbers', assert => {
3026
})
3127

3228
test ('Binary and Sequential search on text file', assert => {
29+
let data = fs.readFileSync('./13-chapter-Searching-Algorithms/paragraph.txt', 'utf8')
30+
.trim()
31+
.split(' ')
32+
3333
const word = 'rhetoric'
3434
let start = process.hrtime()
3535
let position = search.sequentialSearch(data, word)
3636
let end = process.hrtime(start)
37+
3738
assert.ok(position, `${word} is present in the set and is in the position ${position}`)
3839
assert.ok(position, `Sequential search took ${nsTime(end) / 1e9} milliseconds.`)
3940

4041
data = sort(data).insertionSort()
4142
start = process.hrtime()
4243
position = search.binarySearch(data, word)
4344
end = process.hrtime(start)
45+
4446
assert.ok((position >= 0), `${word} is present in the set and is in the position ${position}`)
4547
assert.ok(position, `Binary search took ${nsTime(end) / 1e9} milliseconds.`)
4648
})

0 commit comments

Comments
 (0)