Codewars 7 kyu cats & Shelves JavaScript

Anish RavalAnish Raval
3 min read

All right now we're doing 7 kyu cats and shelves. It's by RealSup & 96% of people like it.

An infinite number of shelves are arranged one above the other in a staggered fashion. The cat can jump up to 3 shelves at the same time: from shelf 1 to shelf 2 or 4 (the cat cannot climb on the shelf directly above its head), according to the illustration:

             ┌────────┐
             │-6------│
             └────────┘

┌────────┐
│------5-│
└────────┘ ┌─────► OK! │ ┌────────┐ │ │-4------│ │ └────────┘ ┌────────┐ │ │------3-│ │
BANG!────┘ ├─────► OK! ▲ |_/| │ ┌────────┐ │ ("^-^) │ │-2------│ │ ) ( │ └────────┘ ┌─┴─┴───┴┬──┘ │------1-│ └────────┘

So, we want to find out how this cat can jump to the one to the Finish Shelf and every time a cat jumps it can either jump 1 or 3 times but i can't do 2 because it'll hit his head.

So, for this one we're going to change the FUNCTION into concise syntax a

const solution = (start, finish) =>

and then we're going to have an arrow like that and the rest of it's out of here.

So, we're going to start by declaring a variable. That's the difference. So,

const solution = (start, finish, difference = finish - start) =>

And then, we're going to come down and say

const solution = (start, finish, difference = finish - start) =>
console.log(

)

So, we can test easy and let's just see what start and finish look like. So, let's say what start looks like. Test it out.

It will show failure. Don't worry we will make it.

Put finish in the parenthesis like this

const solution = (start, finish, difference = finish - start) =>
console.log(
finish
)

Test it out and again, it will again show failure, but it's not ended.

So, what we want to do here is we're going to want to divide the difference by 3 because no matter what it's going to be jumping until it can't jump 3 anymore it's going to jump 3 times. So, we're going to do something like this. We're going to say

const solution = (start, finish, difference = finish - start) =>
console.log(
difference / 3
)

It's not going to be an integer so, we're going to need to round down using the

Math.floor()

function which always rounds down and returns the largest integer less than or equal to a given number. So we're going to say

const solution = (start, finish, difference = finish - start) =>
console.log(
Math.floor(difference / 3)
)

So, once we do that, all we have to do is say

const solution = (start, finish, difference = finish - start) =>
console.log(
Math.floor(difference / 3) + difference % 3
)

So that way if it has 2 left it'll jump 2 left & if it has 1 left it'll jump 1 left. Now update to this piece of code.

const solution = (start, finish, difference = finish - start) =>
Math.floor(difference / 3) + difference % 3

Now, test the code and everything should look good. Now attempt it and submit it.

This one uses the Math.floor function Kata

Math.floor info

1
Subscribe to my newsletter

Read articles from Anish Raval directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anish Raval
Anish Raval

I am a developer from freecodeCamp