cats and a mouse hackerrank solution javascript 1

cats and a mouse hackerrank solution javascript

// cats and a mouse hackerrank solution javascript
function catAndMouse(x, y, z) {
        let catADist = Math.abs(x -z);
        let catBDist = Math.abs(y - z);
        if(catADist > catBDist){
            return "Cat B";
        }else if(catADist < catBDist){
            return "Cat A";
        }else{
            return "Mouse C";
        }
}

Here is what the above code is Doing:
1. We are taking the absolute value of the distance between the cat and the mouse.
2. We are comparing the distance between the two cats and the mouse.
3. If the distance between cat A and the mouse is greater than the distance between cat B and the mouse, then cat B wins.
4. If the distance between cat A and the mouse is less than the distance between cat B and the mouse, then cat A wins.
5. If the distance between cat A and the mouse is equal to the distance between cat B and the mouse, then the mouse wins.

Similar Posts