DEV Community


Posted on

code every day with me

--DAY 5--
Hi, I am going to make #100DaysOfCode Challenge.Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
-Problem: Number Line Jumps
-My solution (javascript):

function kangaroo(x1, v1, x2, v2) {
    while(x1 >= 0){
        //if they in same position 
        if(x1==x2) return 'YES';
        //caculate the distance of 2 kangaroo
        let distance = Math.abs(x1-x2);
        //kangaroo 1 move
        //kangaroo 2 move
        //if the distance between 2 kangaroo longer or no change, they will never meet
        if(Math.abs(x1-x2)>=distance) return 'NO';
Enter fullscreen mode Exit fullscreen mode

-->If you have better solution or any question, please comment below. I will appreciate.

Top comments (0)