0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
imstupidpleasehelp
  • 1,618
  • 2
  • 16
  • 36
  • 1
    So you are aware, in markdown you only need a single backtick on either side of an inline bit of code, such as a variable name; three backticks are to be used when defining a block of code. – Alexander Nied Apr 08 '21 at 00:47
  • 1
    The `=== true` in your conditions isn’t necessary and can be removed. It sounds like you don’t need the loop at all: `x.includes('life')` will be `true` or `false` regardless of what `i` is. – Sebastian Simon Apr 08 '21 at 00:48
  • I want the if statements to apply to every item in the array, that's why I put the loop in. Is there another way to do it? – imstupidpleasehelp Apr 08 '21 at 00:52
  • Are you sure you want to use `else if`? `paul(["life", "eating"])` returns `10` because for every item of the array, you test if `"life"` is included in the array. It always is, so you add `5` and `x.includes('eating')` is never checked because you use `else if` instead of another `if`. See the documentation of [`includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes). What is `paul(["life", "life"])` supposed to return? `5` or `10`? If `5`, then use just `.includes` without a loop. If `10`, _compare_ inside the loop: `x[i] === "life"`. – Sebastian Simon Apr 08 '21 at 00:57
  • `[life life]` should return 10, not 5. Sorry I wasn't clear on this, it should be a cumulative score. – imstupidpleasehelp Apr 08 '21 at 01:09

3 Answers3

1

With the change to requirement, you can approach the problem of counting occurrences like so....

 function paul(x) {            
        let score = 0;   
        x.forEach(s =>  {
          if(s == 'life') score+=5;
          else if(s == 'eating') score +=1;
          else if(s == 'Petes kata') score +=10;
        })            
        return score
    }
    console.log(paul(['life', 'Petes kata', 'eating', 'kata','life']))
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • I guess the question is, if 'life' is in the array twice, would you return 5 or 10 (5 for each time it appears). If 10, you do need that loop right? – Spangle Apr 08 '21 at 01:06
  • It should be 10, if `life` is in the array twice, sorry I i wasn't clear – imstupidpleasehelp Apr 08 '21 at 01:08
  • Thank you. Do you know why this is returning undefined when presented with a long array? I am testing an array with a length of 30+ and it is returning `0 undefined` – imstupidpleasehelp Apr 08 '21 at 01:39
  • nope, I'd need to see the array, I doubt it has anything to do with the length of the array – Keith Nicholas Apr 08 '21 at 01:42
  • The arrays look like this: `eating,kata,eating,kata,eating,eating,eating,life,Petes kata,kata,life,kata,kata,life,eating,kata,eating` You can also test it out yourself here https://www.codewars.com/kata/57ee31c5e77282c24d000024/train/javascript – imstupidpleasehelp Apr 08 '21 at 01:51
  • @imstupidpleasehelp The function is not returning `undefined`. That’s just the return value of `console.log` that you see in a REPL. See [node.js displays “undefined” on the console](https://stackoverflow.com/q/8457389/4642212). It’s returning `0`, because this isn’t the expected input. An array of strings looks like `["kata", "eating", "etc."]`, not like `kata,eating,etc.`. – Sebastian Simon Apr 08 '21 at 02:07
  • Then I suppose that's the fault of the challenge and not Keith's code, Thank you all for your help! – imstupidpleasehelp Apr 08 '21 at 02:20
1

Maybe you misunderstood the use of Array's .includes() method. At the moment you're using a for-loop to iterate over each element in the array manually and try to execute .includes() on x - a reference to the input array. Actually that's not neccessary as the .includes() method automatically goes over each element in your array.

Something like this is sufficient:

let arr = ["life", "Petes kata", "eating", "kata"];

function paul(x) {
  let returnString = 0;

  if (x.includes('life')) {
    returnString += 5;
  }
  if (x.includes('eating')) {
    returnString += 1;
  }
  if (x.includes('Petes kata')) {
    returnString += 10;
  }

  return returnString;
}

console.log(paul(arr));
obscure
  • 11,916
  • 2
  • 17
  • 36
0

I don't understand why you use for statement on your code. Maybe you wrote wrong code. Please try like this.

for (i = 0; i < x.length; i++) {
   if (x[i].includes('life')) {
     returnString += 5
   } else if (x[i].includes('eating')) {
     returnString += 1
   } else if (x[i].includes('Petes kata')) {
     returnString += 10
   }
}