Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
A text-processing utility is a short pipeline with explicit stages. Clean the boundary whitespace, split on spaces, and join the parts with a new separator. split creates an array, which you will study later; here it is an intermediate collection of text pieces. Multiple consecutive spaces require a more deliberate splitting rule. The examples use single separators so the first version has a clear, limited contract.
const text = "learn by doing";
console.log(text.trim().split(" ").join("-"));learn-by-doing
The original spaces become array boundaries and the join inserts hyphens between pieces.
const text = "learn by doing";Declare text with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
console.log(text.trim().split(" ").join("-"));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
const text = "hello world";
console.log(text.trim().split(" ").join("-"));hello-world
The original spaces become array boundaries and the join inserts hyphens between pieces.
const text = "hello world";This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
console.log(text.trim().split(" ").join("-"));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
const text = "one";
console.log(text.trim().split(" ").join("-"));one
The original spaces become array boundaries and the join inserts hyphens between pieces.
const text = "one";This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
console.log(text.trim().split(" ").join("-"));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
const text = "learn by doing";
console.log(text.trim().split(" ").join(""));const text = "learn by doing";
console.log(text.trim().split(" ").join("-"));State how repeated whitespace and punctuation should be handled before generalizing a text tool.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.