Difference between Built-in method Slice in JavaScript and Ruby

Hima Chitalia
Coffee and Codes
Published in
4 min readOct 9, 2017
Slice Method — JavaScript v/s Ruby

Coding is all about practice, patience, and dedication. More you do, more you want to do. JS has always amazed and attracted me. For me, it has a magic. To be master of it, started solving katas on the Codewars.com everyday.

Yesterday, while solving a kata Largest 5 digit number in a series, I used built-in JS method slice. I also used the same built-in method in Ruby to solve the same kata. Today we will discuss a small but sharp difference between the result of slice method between Ruby and JavaScript.

For the example purpose, we will just use the same kata of codewars. Here, we need to find the greatest sequence of 5 digits. We will first see what JavaScript slice gives us when we iterate over the string.

function solution(digits){
var largest= 0;
var now = 0;

for(let i = 0; i < digits.length - 4; i++){
now = digits.slice(i, (i + 5))
console.log("Value of variable now is " + now + " when value of i is " + digits[i])
largest = Math.max(largest, now)
}
return largest
}

Here, for this blog post, we will pay attention to now = digits.slice(i, (i + 5)) line. If you log out the value of variable now at this point for the string “12345678909125”, you will see these lines in console.

Value of variable now is 12345 when value of i is 1
Value of variable now is 23456 when value of i is 2
Value of variable now is 34567 when value of i is 3
Value of variable now is 45678 when value of i is 4
Value of variable now is 56789 when value of i is 5
Value of variable now is 67890 when value of i is 6
Value of variable now is 78909 when value of i is 7
Value of variable now is 89091 when value of i is 8
Value of variable now is 90912 when value of i is 9
Value of variable now is 09125 when value of i is 0

Iteration will stop after 0 because we asked it to run till i is less than digits.length — 4. We need largest 5 digit number so it doesn’t make sense to let it iterate over last 4 digits. Anyway, here point I am trying to make is in the line when you say digits.slice(i, (i + 5)), it will slice digits from i to (i +5). So, if i is at index 2, it will slice 5 digits from index 2 to 7.

Now, we will look at Ruby.

We will try to solve same kata in Ruby using ruby while loop.

def solution(digits) 
@largest = 0;
@i = 0;
@length = digits.length — 4;
while @i < @length do
@current_array = []
@now = digits.slice(@i, (@i + 5))
puts "Value of variable now is #{@now} when value of @i is #{digits[@i]}"
@current_array.push(@now.to_i)
@current_array.push(@largest)
@i += 1
@largest = @current_array.max
end
@largest
end

If you try to put out the value of @now and @i for all of the iterations, you will see these lines:

Value of variable now is 12345 when value of @i is 1 
Value of variable now is 234567 when value of @i is 2
Value of variable now is 3456789 when value of @i is 3
Value of variable now is 45678909 when value of @i is 4
Value of variable now is 567890912 when value of @i is 5
Value of variable now is 678909125 when value of @i is 6
Value of variable now is 78909125 when value of @i is 7
Value of variable now is 8909125 when value of @i is 8
Value of variable now is 909125 when value of @i is 9
Value of variable now is 09125 when value of @i is 0

Here again, we stopped iteration before last 4 digits as we need max 5 digit number. But the point to note here is the value of @now, it will start with the index that @i is pointing to. But it won’t stop at the index where (@i +5) will point to. Here, it will slice as many numbers the second parameter (@i +5) will result in.

That means if @i = 2, (@i + 5) will be 7. So, @now will have total 7 numbers starting from index 2. Which will make the value of @now = 3456789.

It won’t give us the desired 5 digit number. To make the story short, in the second parameter if we write just 5 instead of (@i + 5), it will give as we need. Slice will exactly slice 5 numbers from the index @i will point to.

Summary:

The main difference between slice method of JavaScript is how you slice value. In JavaScript second parameter in slice method is index number it needs to slice up to. While in Ruby, the second parameter is a number of digits it needs to slice starting from the i is pointing to.

The difference is really small but significant enough to change output completely.

Any suggestion or question, please feel free to write me at hima.chhag@gmail.com or in comment section below.

Happy Coding!

--

--