DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#34 -Shortest code: Bug in Apple - CodeWars Kata (6 kyu)

Instructions

shortest code: Bug in Apple
(Code length limit: 80 chars)

This is the challenge version of coding 3min series. If you feel difficult, please complete the simple version

Task
Find out "B"(Bug) in a lot of "A"(Apple).

There will always be one bug in apple, not need to consider the situation that without bug or more than one bugs.

input: string Array apple
output: Location of "B", [x,y]

Code length calculation
In javascript, we can't get the user's real code, we can only get the system compiled code. Code length calculation is based the compiled code.

For example:
If you typed sc=x=>x+1
after compile, it will be:sc=function(x){return x+1;}


My solution:

function sc(a){
  for (i = 0; i < a.length; i++)
  {
    j = a[i].indexOf('B')
    if (j > -1) return [i, j]
  }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

First I used a loop to iterate in the first array that contained the other arrays, and then inside it I declarated "j", that contained the index of 'B' inside the array that is being iterated, and after that I added a conditional that if the index of 'B' is higher than -1, it means that there is a 'B' element so I just returned the last result that is an array with the index of the array being iterated so I get the row and "j" that is the index of 'B', so I get the column


What do you think about this solution? 👇🤔

My Github
My twitter
Solve this Kata

Top comments (0)