DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#6 - Incrementer CodeWars Kata (7 kyu)

Instructions:

Task
Given an input of an array of digits, return the array with each digit incremented by its position in the array: the first digit will be incremented by 1, the second digit by 2, etc. Make sure to start counting your positions from 1 ( and not 0 ).

Your result can only contain single digit numbers, so if adding a digit with its position gives you a multiple-digit number, only the last digit of the number should be returned.

Notes:
return an empty array if your array is empty
arrays will only contain numbers so don't worry about checking that

Examples:

[1, 2, 3] --> [2, 4, 6] # [1+1, 2+2, 3+3]
[4, 6, 9, 1, 3] --> [5, 8, 2, 5, 8] # [4+1, 6+2, 9+3, 1+4, 3+5]
# 9+3 = 12 --> 2

My solution:

function incrementer(nums) { 
  if(nums == []) return[]
  return nums.map((n,i)=> (i + n + 1)%10)
}

Enter fullscreen mode Exit fullscreen mode

Explanation
I started with a conditional, if the array is empty it'll return an empty array, if not it'll return the result, for the result I mapped the nums input, taking each number and its index as parameters, then I summed the index + number + 1, I summed one because I have to start counting the postions from 1, so every index needs to be summed one, then to that result I used the % operator so it returns only the remainder of the division and I can get only the second digit of the result because it is divided by 10

Comment how would you solve this kata and why? 👇🤔

My Github
My twitter
Solve this Kata

Discussion (0)