thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

22.05 · Final JavaScript challenges / Watch · 13 min

Find a longest word with Rob

Track a candidate as you visit words, and keep the first one when lengths tie.

Activity 5 of 8 in Final JavaScript challenges

All courses

JavaScript Basics / 22.05

Find a longest word with Rob

Not started

Track a candidate as you visit words, and keep the first one when lengths tie.

More options

Your lesson progress

Reading & walkthrough: not startedGuided practice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob compares coding with six letters to ninja with five and selects coding from I am a coding ninja.
Rob’s comic strip · Final JavaScript challenges

ORIGINAL WALKTHROUGH · 12:43

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

script.js
script.jsjavascript · example
/*
Write largestWord(str), returning the longest word in a string. If several words have the same greatest length, return the first. Start with I am a coding ninja, which should return coding. Also try red sun, which should return red. For this companion, split on spaces, keep punctuation in tokens and use basic Latin text.
*/

function largestWord(str) {
  let largest = "";
  // Split the phrase and update your candidate here.
  return largest;
}

// Log largestWord("I am a coding ninja") when ready.

Find the first longest word

Work through 5 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

5 steps · Saved work · Instant feedback

YOUR TURN

Try the examples on this page

Choose Load this code into the editor beside an example, then edit and Run code here. Predict what will print before running it. This is an exploration workspace, so there is no pass/fail challenge. Its saved work stays separate from the next practice activity.

Load an example, edit it and run it. Word wrap is enabled.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
const words = "I am a coding ninja".split(" ");
for (const word of words) {
  console.log(word + ": " + word.length);
}

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Open the code-along notes

Inspect the words and their lengths

The original challenge asks for the longest word, returning the first one if several words share the greatest length. In the original phrase, coding has six letters and ninja has five.

Run this length inventory, then trace a separate candidate as you visit I, am, a, coding and ninja. The candidate should change to I, then am, then coding; it should stay coding at ninja.

Inspect the words and their lengths
example.jsjavascript · example
const words = "I am a coding ninja".split(" ");
for (const word of words) {
  console.log(word + ": " + word.length);
}

A second input tests the tie rule

For red sun, both lengths are 3. Keep red because it appears first. Use this case to check the comparison rather than assuming one passing example proves the whole function.

The original assignment accepted a JSFiddle or GitHub URL. Here, signed-in learners can submit their code directly for instructor review. The separate workspace starts with the original phrase and a runnable function stub.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

The first example alone cannot reveal the tie bug. Using > keeps the first equal-length candidate; >= replaces it with a later tie. Splitting on a space yields empty tokens for repeated spaces; these never beat the initial empty candidate or a real word. This is a simple token exercise, not a punctuation-aware word parser.