Blog

9.6.5 Coordinates (CodeHS JavaScript): How the Mouse Position Exercise Works

Muhammad Ubaid 5 min read
9.6.5 Coordinates

9.6.5 Coordinates is a five-point exercise in CodeHS’s Introduction to Computer Science in JavaScript (Golden, 2022) course. It belongs to Lesson 9.6, Mouse Events: Mouse Moved, and sits between 9.6.4 Colorful Drag to Paint and 9.6.6 Target. CodeHS keeps the full assignment text behind a login, so your own instructions are the final word on what the grader expects. This guide covers the skills the lesson teaches: how a program learns where the mouse is, and how it shows that information on the canvas.

What the exercise is meant to teach

The public lesson material introduces three tools. The first is mouseMoveMethod, which tells the program to react whenever the cursor moves. The second is the event object, usually named e, which carries the pointer’s position. The third is the Text graphic, which can display words and numbers. Study guides written by third parties describe the exercise as showing the cursor’s position on the canvas as it moves. CodeHS has not publicly confirmed that wording, so check it against your own prompt, including any required text format or screen position.

How mouseMoveMethod and the event object work together

You register the reaction once, inside start(), by passing the name of a function to mouseMoveMethod. From then on, CodeHS calls that function every time the mouse moves, however slightly, and hands it the event object. The lesson itself explains that e.getX() and e.getY() return the horizontal and vertical position of the pointer. That means the function you write runs dozens of times per second during normal movement, which is the single idea behind most bugs in this exercise.

The function name goes in without parentheses. Writing mouseMoveMethod(showPosition()) runs the function once, immediately, before any event exists, so e is missing and the program fails. Writing mouseMoveMethod(showPosition) hands over the function itself, to be called later.

Updating one label instead of stacking new ones

Because the callback fires constantly, creating a fresh Text object inside it without cleaning up leaves a smear of old labels on the canvas. There are two clean ways to avoid that. You can remove the previous label before adding a new one, which works but repeats a lot of effort. The approach CodeHS’s own lesson uses for its circle counter is better: build the text once during setup, keep it in a variable, and change its words later with setText.

var label;

function start() {
    label = new Text("", "14pt Arial");
    label.setPosition(10, 25);
    add(label);
    mouseMoveMethod(showPosition);
}

function showPosition(e) {
    label.setText(e.getX() + ", " + e.getY());
}

Treat this as a pattern rather than a finished answer, because your assignment may ask for a particular wording, a different spot on the screen, or extra behavior. One quirk is worth knowing: the label stays empty until the mouse first moves, so a starting message such as a placeholder makes the screen look less broken at load time.

Mistakes that look like bigger problems than they are

What you seeLikely cause
Error as soon as the program runsParentheses after the function name inside mouseMoveMethod
Numbers that don’t match the cursorCallback written without the e parameter, so the position can’t be read
A smudge of overlapping textA new Text created on every move with nothing removed
Output only appears when you click or hold the buttonmouseClickMethod or mouseDragMethod used instead of the move method
A single number like 205 instead of 120, 85Two numbers added together because no string sits between them
Label partly cut offPosition set too close to the canvas edge

The additive trap deserves a closer look. In JavaScript, e.getX() + e.getY() with nothing but numbers does arithmetic, so a cursor at 120 and 85 would show 205. Putting a piece of text such as ", " between the two values forces them to join as words instead.

Reading the numbers correctly

Screen graphics count from the top-left corner of the canvas, so the point (0, 0) is the upper left, x grows toward the right, and y grows downward. This surprises anyone used to math class graphs where up is positive. If you want to place something relative to the far side or the middle, getWidth() and getHeight() report the canvas size, which is how the lesson’s counter example centers its label near the top of the screen.

Where this fits in the bigger picture

The shape you practice here, a registration line in start() plus a callback that receives e, repeats across the whole lesson. 9.6.3 and 9.6.4 draw circles along the pointer’s path, and the lesson shows that swapping in mouseDragMethod limits drawing to when a button is held. 9.6.6 builds on the same event data, and the following lessons on drawing lines and key events reuse the idea with different triggers. Getting comfortable with live coordinates now pays off in the later game projects.

Questions students ask about this exercise

Is the number always 9.6.5?

No. Numbering follows each course. In CodeHS’s Video Game Design in JavaScript course, for example, an activity with the same title is numbered 9.5.5, so a classmate in another course may have a different label for the same task.

Can I just copy a solution?

Copying skips the part that matters. The grader checks your result, but later exercises assume you can wire up an event callback on your own, so it is worth typing and testing the pattern until you can explain each line.

Explore More

lamps4u: Lighting Products, Online Deals, and Smart Buying Guide

Muhammad Ubaid

I’m Muhammad Ubaid, founder of YBR Magazine. I research and write detailed guides on America’s National Parks — covering entry fees, permits, best times to visit, and planning tips — using official NPS sources and up-to-date information.

Leave a Reply

Your email address will not be published. Required fields are marked *