I need help to fix my code. as i failed the last one no 9.
Q: Fill in the if_then_elseif() function to create the desired array using a for loop and an if then else statement. The goal is to create an output table equivalent to the input table as per the following correspondence {"1", "2", "3", "4", "5", "6", "7", "8", "9"} =>> {"a", "b", "c", "d", "e", "f", "n", "n", "n"} ]]
require "testwell"
function foo(arg)
--arg is the table of numbers
local out = {}
local n = #arg
-- Put your code between here ****************
for i = 1,#arg,1 do
if i==1 then out[i] = "a"
elseif i==2 then out[i] = "b"
elseif i==3 then out[i]= "c"
elseif i==4 then out[i] = "d"
elseif i==5 then out[i]= "e"
elseif i==6 then out[i] = "f"
elseif i==7 then out[i] = "n"
elseif i==8 then out[i]= "n"
elseif i==9 then out[i] = "n"
else print("invalid input")
end
end
-- and here **********************************
return out
end
is(foo({1,2}), {"a", "b"}, 'numbers to letters 1')
is(foo({1,2,3}), {"a", "b", "c"}, 'numbers to letters 2')
is(foo({1,2,3,4}), {"a", "b", "c", "d"}, 'numbers to letters 3')
is(foo({1,2,3,4,5}), {"a", "b", "c", "d", "e"}, 'numbers to letters 4')
is(foo({1,2,3,4,5,6}), {"a", "b", "c", "d", "e", "f"}, 'numbers to letters 5')
is(foo({1,2,3,4,5,6,7}), {"a", "b", "c", "d", "e", "f", "n"}, 'numbers to letters 6')
is(foo({1,2,3,4,5,6,7,8}), {"a", "b", "c", "d", "e", "f", "n", "n"}, 'numbers to letters 7')
is(foo({1,2,3,4,5,6,7,8,9}), {"a", "b", "c", "d", "e", "f", "n", "n", "n"}, 'numbers to letters 8')
is(foo({9,8,7,6,5,4,3,2,1}), {"n", "n", "n", "f", "e", "d", "c", "b", "a"}, 'numbers to letters reversed')
report()
``````````````````````
the output i received is
`````````
Program 'lua.exe' started in 'C:\Users\Marian\Downloads\ZeroBraneStudio\myprograms' (pid: 6032).
ok 1 - numbers to letters 1
ok 2 - numbers to letters 2
ok 3 - numbers to letters 3
ok 4 - numbers to letters 4
ok 5 - numbers to letters 5
ok 6 - numbers to letters 6
ok 7 - numbers to letters 7
ok 8 - numbers to letters 8
not ok 9 - numbers to letters reversed
# Failed test 'numbers to letters reversed'
# in ...\ZeroBraneStudio\myprograms\290-A2\05-e-if then else.lua at line 54.
# got: {"a", "b", "c", "d", "e", "f", "n", "n", "n"} (table)
# expected: {"n", "n", "n", "f", "e", "d", "c", "b", "a"} (table)
1..9 # Looks like you passed 8 and failed 1 test of 9 (8/0/9).
Program completed in 0.17 seconds (pid: 6032).
`````````````````````
My personal recommendation:
function foo( arg )
--arg is the table of numbers
local map = {'a', 'b', 'c', 'd', 'e', 'f', 'n', 'n', 'n'}
local out = {}
local n = #arg -- not needed.
-- Put your code between here ****************
for key, value in pairs( arg ) do -- note the associative arg is also handled.
out[key] = map[value] or print 'Invalid input' -- no need of if's.
end
-- and here **********************************
return out
end
But if you must use if
's ang arg
is a purely indexed array:
function foo( arg )
--arg is the table of numbers
local out = {}
local n = #arg
-- Put your code between here ****************
for i = 1, n do
if arg[i] == 1 then out[i] = 'a'
elseif arg[i] == 2 then out[i] = 'b'
elseif arg[i] == 3 then out[i] = 'c'
elseif arg[i] == 4 then out[i] = 'd'
elseif arg[i] == 5 then out[i] = 'e'
elseif arg[i] == 6 then out[i] = 'f'
elseif arg[i] == 7 or arg[i] == 8 or arg[i] == 9 then out[i] = 'n'
else print 'Invalid input' end
end
-- and here **********************************
return out
end
P.S. I hope your teacher doesn't read StackOverflow.