javascript - Why is the Argument taken as an Object and not as an Array? -
why argument taken object , not array??
question description:
consider array of sheep sheep may missing place. need function counts number of sheep present in array (true means present).
for example,
[true, true, true, false, true, true, true, true , true, false, true, false, true, false, false, true , true, true, true, true , false, false, true, true]
the correct answer 17.
my solution:
function countsheeps(arrayofsheeps) { var num=0; for(var i=0; i<arrayofsheeps.lenght(); i++) { if(arrayofsheeps[i]==true){ num=num+1; } } return num; }
test cases:
var array1 = [true, true, true, false, true, true, true, true , true, false, true, false, true, false, false, true , true, true, true, true , false, false, true, true ]; test.expect(countsheeps(array1) == 17, "there 17 sheeps in total")
output:
typeerror: object true,true,true,false,true,true,true,true,true,false,true,false,true,false,false,true,true,true,true,true,false,false,true,true has no method 'lenght' @ countsheeps 0 passed 0 failed 1 errors process took 110ms complete
p.s. newbie js.
based on comments should correct lenght()
length
function countsheeps(arrayofsheeps) { var num=0; for(var i=0; i<arrayofsheeps.length; i++) { if(arrayofsheeps[i]==true){ num++; } } return num; }
Comments
Post a Comment