Skip to content

Commit 5274ede

Browse files
committed
add new test to reduce_right
1 parent cbd8d49 commit 5274ede

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

array.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ array = {
213213
local _memo = memo
214214

215215
if _memo == nil then
216-
initialIndex = 2
217-
_memo = obj[1]
216+
initialIndex = 2
217+
_memo = obj[1]
218218
end
219219

220220
for i=initialIndex, #obj do

test.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ test('reduce_right should concatenate all items starting from right to left', fu
154154
a.equal(result, 'edcba')
155155
end)
156156

157+
test('reduce_right should return 100', function(a)
158+
local result = array.reduce_right({ 20, 30, 40 }, function(memo, value)
159+
return memo + value
160+
end, 10)
161+
162+
a.equal(result, 100)
163+
end)
164+
165+
test('reduce_right', function(a)
166+
local result = array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
167+
return memo .. value
168+
end)
169+
170+
a.equal(result, 'edcba')
171+
end)
172+
157173
test('sum should return sum of the values in table', function(a)
158174
local result = array.sum({10, 20, 30, 40, 50})
159175
a.equal(result, 150)

0 commit comments

Comments
 (0)