Please enable JavaScript to use CodeHS

Want to receive Question of the Day updates? Subscribe Here

JavaScript Question of the Day March 18, 2025

  1. Incorrect Correct No Answer was selected Invalid Answer

    The following code creates two circles that start on the left and right side of the canvas and move towards each other in the middle of the canvas:

    let leftCircle;
    let rightCircle;
    let dx = 5;
    const RADIUS = 30;
    
    function main() {
        leftCircle = new Circle(RADIUS);
        leftCircle.setPosition(RADIUS, getHeight() / 2);
        leftCircle.setColor("blue");
        add(leftCircle);
    
        rightCircle = new Circle(RADIUS);
        rightCircle.setPosition(getWidth() - RADIUS, getHeight() / 2);
        rightCircle.setColor("red");
        add(rightCircle);
    
        setTimer(draw, 50);
    }
    
    function draw() {
        crashCheck(); // choose the correct version of this function
        leftCircle.move(dx, 0);
        rightCircle.move(-dx, 0);
    }
    
    main();
    JavaScript

    What would be the correct implementation of the crashCheck function if we wanted the circles to move in the opposite direction once they collide?