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
-Detail: https://www.hackerrank.com…


This content originally appeared on DEV Community and was authored by duccanhole

--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
-Detail: https://www.hackerrank.com/challenges/kangaroo/problem
-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
        x1+=v1;
        //kangaroo 2 move
        x2+=v2;
        //if the distance between 2 kangaroo longer or no change, they will never meet
        if(Math.abs(x1-x2)>=distance) return 'NO';
    }
}

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


This content originally appeared on DEV Community and was authored by duccanhole


Print Share Comment Cite Upload Translate Updates
APA

duccanhole | Sciencx (2021-10-23T04:13:24+00:00) code every day with me. Retrieved from https://www.scien.cx/2021/10/23/code-every-day-with-me/

MLA
" » code every day with me." duccanhole | Sciencx - Saturday October 23, 2021, https://www.scien.cx/2021/10/23/code-every-day-with-me/
HARVARD
duccanhole | Sciencx Saturday October 23, 2021 » code every day with me., viewed ,<https://www.scien.cx/2021/10/23/code-every-day-with-me/>
VANCOUVER
duccanhole | Sciencx - » code every day with me. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/23/code-every-day-with-me/
CHICAGO
" » code every day with me." duccanhole | Sciencx - Accessed . https://www.scien.cx/2021/10/23/code-every-day-with-me/
IEEE
" » code every day with me." duccanhole | Sciencx [Online]. Available: https://www.scien.cx/2021/10/23/code-every-day-with-me/. [Accessed: ]
rf:citation
» code every day with me | duccanhole | Sciencx | https://www.scien.cx/2021/10/23/code-every-day-with-me/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.