I am doing a coding challenge where you are given an array of string like so: [life, Petes kata, eating, kata]
and your code is supposed to return a value based of of what's in the string:
kata = 5
function paul(x) {
let returnString = 0
var i;
for (i = 0; i < x.length; i++) {
if (x.includes('life') === true) {
returnString += 5
} else if (x.includes('eating') === true) {
returnString += 1
} else if (x.includes('Petes kata') === true) {
returnString += 10
}
}
return returnString
}
At the moment my function works fine if there is only one word in the array. life
returns 5, and Petes kata
returns 10, as it should.
However when there is more than 1 word in the array, the function will always return 0, and I am not sure why.
I thought that wrapping the if statements with a for loop would make the function add to returnString several times, and if it were only working on the first word I could also understand that, but returning 0 has been puzzling me for a while.