TechDojoPilot Cohort Week 2 · Day 6

Pilot CohortPhase 1Week 2Day 6

JavaScript That React Needs, and Choosing a Problem for Your Own Product

Your day, about 6 hours

  1. Learn2 hSections 4 to 9
  2. Build2 hSection 10
  3. Flex1 hJournals, then P1 problems, section 11
  4. Stand-up1 hLive, 9 pm WAT
01

Daily theme and context

Week 1 ended with a styled Tracker page, one small JavaScript interaction, and a live link on GitHub Pages. Week 2 turns that page into a real frontend application, and by Friday it becomes a Next.js app. Before that can happen, the JavaScript underneath has to be solid. React is mostly ordinary JavaScript: objects, arrays, functions and a few array methods used over and over. Today teaches exactly that subset, through the Tracker's own data.

Today also starts the search for your own product, P1, which you will build from Week 3 to Week 6. It starts the way Day 1 did: with problems, not apps.

This is Week 2 of the Pilot Cohort's six-week Learn phase ("Frontend"), Day 6 of 30 learning days. Day 6 starts at the Monday (23 Nov) 9 pm class; its work goes into Quest 2, due by Sunday 29 Nov, 11:59 pm WAT. It has four blocks, about 6 hours, done whenever suits you except the stand-up: Learn (2 hours, covered in full below), Build (2 hours, section 10: the Learner Project Tracker), Flex (1 hour: your three journals first, then the P1 problem list in section 11), and the live stand-up at 9 pm WAT (1 hour).

02

Learning objectives

By the end of today, the learner should be able to:

  1. Store related data in an object and a list of them in an array, and read values out of both.
  2. Write a function, including the short arrow-function form, that takes inputs and returns a result, and make decisions with if/else and the ternary (? :).
  3. Use map, filter and find to transform, select and look up items in an array, and say what each one returns.
  4. Turn an array of objects into a list on the page, safely, with createElement and textContent.
  5. Split code across files with export and import, and run the page through a local server.
  6. Read an error in the browser console, find the line it points to, and fix it.
  7. List three real problems and test each against the four problem questions for P1.
03

Prerequisite knowledge

Week 1's Tracker page, published on GitHub Pages, and Day 5's first look at variables, functions and the DOM.

04

TechDojo core lesson

35 minutes

Objects: one thing, described by its parts

An object groups related values under names. A learner in the Tracker is one object:

const learner = { id: 1, name: "Ada", attendance: 9, sessions: 10, status: "active" };
console.log(learner.name);        // "Ada"
console.log(learner["status"]);   // "active"

Each name (id, name) is a property. You read a property with a dot, or with square brackets when the name is in a variable.

Arrays: many things, in order

An array is an ordered list. A cohort is an array of learner objects:

const learners = [
  { id: 1, name: "Ada", attendance: 9, sessions: 10 },
  { id: 2, name: "Tunde", attendance: 6, sessions: 10 },
  { id: 3, name: "Zainab", attendance: 10, sessions: 10 }
];
console.log(learners.length);     // 3
console.log(learners[0].name);    // "Ada" (counting starts at 0)

learners.push(newLearner) adds an item to the end. It changes the array itself, which is fine in plain JavaScript; Day 9 explains why React needs a different approach.

Almost every screen you will build is "an array of objects, shown as a list". Get comfortable with this shape; it is the shape of databases in Week 3 too.

const and let

Use const when the name will always point to the same thing, and let when you will reassign it. const doesn't freeze an object or array: you can still change what's inside. It only stops you pointing the name at something else. Start with const and switch to let only when you need to reassign.

Functions and arrow functions

A function takes inputs (parameters) and returns a result:

function attendanceRate(learner) {
  return Math.round((learner.attendance / learner.sessions) * 100);
}
const attendanceRate2 = (learner) => Math.round((learner.attendance / learner.sessions) * 100);

Both lines do the same job. The second is an arrow function; when its body is a single expression, the result is returned automatically. React code uses arrow functions constantly.

Template literals: building text from values

Text written between backticks can hold values inside ${ }:

const line = `${learner.name}: ${attendanceRate(learner)}% attendance`;
// "Ada: 90% attendance"

This is easier to read than joining pieces with +, and you'll use it for every label and message today.

Making decisions: if, else and the ternary

A learner who hasn't had any sessions yet has sessions: 0, and dividing by zero gives NaN ("not a number"), which would appear on the page as "NaN%". An if handles it:

function attendanceRate(learner) {
  if (learner.sessions === 0) {
    return 0;   // no sessions yet, so nothing to divide
  }
  return Math.round((learner.attendance / learner.sessions) * 100);
}

Conditions use comparisons (<, >=, ===, !==) and can be combined with && (and), || (or) and ! (not). When you need one of two values, the ternary is the short form of if/else:

const status = rate < 70 ? "at risk" : "on track";

Read it as: "if rate < 70, then "at risk", otherwise "on track"." React code uses it constantly, because it fits inside a single expression. One more thing to know: undefined counts as false in a condition, so if (!found) means "if nothing was found".

The three array methods you will use every day

  • map makes a new array of the same length by transforming each item: learners.map(l => l.name) gives ["Ada", "Tunde", "Zainab"]. In React, map is how a list of data becomes a list of things on screen.
  • filter makes a new, possibly shorter array of the items that pass a test: learners.filter(l => attendanceRate(l) < 70) gives only the learners below 70%.
  • find returns the first single item that passes a test, or undefined if none does: learners.find(l => l.id === 2) gives Tunde's object.

None of the three changes the original array. That matters a lot in React (Day 9).

  • mapA new array, same length: every item transformed. Data becomes a list on screen.
  • filterA new array, possibly shorter: only the items that pass the test.
  • findOne item: the first that passes the test, or undefined if none does.
None of the three changes the original array.That's what makes them safe to use in React (Day 9).

Pulling values out of an object: destructuring

const { name, attendance } = learner;           // same as learner.name and learner.attendance
const greet = ({ name }) => `Welcome, ${name}`;  // takes a learner, uses only its name

You'll see the second form in every React component from Day 8: the curly braces in the parameter pull named values out of the object passed in.

===, not ==

Compare with ===. It checks the value and the type, so 2 === "2" is false. The older == converts types first and causes surprises. Remember the Chop Express entry task: values from a form are text, so convert them with Number(...) before doing maths.

==Converts types first

2 == "2" is true, which hides bugs from form input.

===Checks value and type

2 === "2" is false. Convert with Number(...) first, then compare.

From data to the page

On Day 5 you selected one element and changed it. Showing a list is the same idea, repeated for every item: make an element for each object, then put them all into the page.

const list = document.querySelector("#learner-list");   // a <ul id="learner-list"> in your HTML

const items = learners.map((learner) => {
  const rate = attendanceRate(learner);
  const li = document.createElement("li");
  li.textContent = `${learner.name}: ${rate}%`;
  if (rate < 70) {
    li.classList.add("at-risk");
  }
  return li;
});

list.replaceChildren(...items);

Line by line: map turns each learner object into a new <li> element; textContent sets its text; classList.add gives it a CSS class you can style; replaceChildren empties the list and puts the new items in. The ... in front of items spreads the array out into separate values, because replaceChildren takes them one by one (you'll meet ... again on Day 9). Run the code again whenever the data changes and the whole list is redrawn from the data. That is exactly the job React will do for you from Day 8.

  1. 1DataAn array of learner objects in data.js
  2. 2mapOne <li> for each learner, text set with textContent
  3. 3replaceChildrenEmpties the list and puts the new items in
  4. 4The pageShows whatever the data says

Change the data, run it again, and the list is redrawn. From Day 8, React does this for you.

Reading what someone typed

For today's lookup you need one pattern: read an input's value when a button is clicked.

const input = document.querySelector("#lookup-id");
const button = document.querySelector("#lookup-button");

button.addEventListener("click", () => {
  const id = Number(input.value);   // what was typed is always text, so convert it
  console.log(id);
});

Give the input a visible <label> (Day 3). Day 7 covers events and forms properly; finding the learner and showing the result is yours to write.

Splitting code into files: export and import

As code grows, you split it into files. One file exports what others may use; another imports it:

// data.js
export const learners = [ /* ... */ ];
// app.js
import { learners } from "./data.js";

In the HTML, load the entry file as a module: <script type="module" src="app.js"></script>.

Why you now need a local server

Browsers block modules (and, tomorrow, fetch) on pages opened straight from your disk (a file:// address). The fix is to serve the folder through a small local web server. The simplest way: install the Live Server extension in VS Code, then right-click index.html and choose "Open with Live Server". The page opens at an address like 127.0.0.1, the way a real server would deliver it. GitHub Pages already serves your page properly, so it keeps working online, with two catches. Always write the full file name in an import, including .js ("./data.js", not "./data"). And match capital letters exactly: your laptop may not mind if the file is Data.js and the import says data.js, but GitHub Pages does, and the page breaks only online.

file://Opened straight from your disk

The browser blocks import, and tomorrow fetch.

127.0.0.1Served by Live Server

Delivered the way a real server, or GitHub Pages, delivers it.

Reading an error

When something breaks, open the browser's DevTools (F12, or right-click → Inspect) and the Console tab. A red error names the problem and the file and line, for example app.js:14. Click it to jump to the line. Most early errors are one of three things: a misspelt name (learner.nmae), using something that is undefined (learners[5].name when there are only three), or a wrong path in an import. Read the message slowly before changing anything; it usually says exactly what is wrong.

When the page shows something unexpected but there's no red error, check the values instead of guessing: console.log(found) prints a value, and console.table(learners) shows an array of objects as a table.

05

Video resource

15 minutes
Web Dev Simplified, "8 Must Know JavaScript Array Methods" Open on YouTube

Watch it once in full, then use the rest of the block to type the examples for map, filter and find yourself in the browser console, using the learners array from the core lesson instead of the video's data.

Guided viewing questions:

  1. Which of the methods in the video return a new array, and which return a single value?
  2. What does find give back when nothing matches, and why does that matter before you read a property from the result?
  3. Which method would you use to get the names of every learner below 70% attendance? (Hint: you may need two methods, one after the other.)
06

Reading resource

15 minutes
javascript.info, "Array methods"javascript.info

Read the sections on find, filter and map (under "Iterate" and "Searching in array" and "Transform an array"). Skip splice, reduce and the rest for now.

javascript.info, "Arrow functions, the basics"javascript.info

Read it in full; it is short.

Guided reading questions:

  1. filter and find both take a test function. What is the exact difference in what they return?
  2. When does an arrow function need curly braces and a return, and when doesn't it?
  3. Write, from memory, an arrow function that takes a learner and returns true if they are below 70% attendance.
07

Interactive exercise

40 minutes

Write these by hand in a practice folder (not the Tracker yet): practice.html, plus practice-data.js exporting a learners array and practice.js importing it, loaded as a module. Run it through Live Server and check results in the console.

  1. 8 min Create the learners array from the core lesson with at least five learners, including one with sessions: 0. Write attendanceRate(learner) with the zero check, and log a line for each learner with a template literal, such as "Ada: 90%".
  2. 8 min Use filter to get learners below 70%, then map to turn them into a list of names. Log it. Then log every learner's status ("at risk" or "on track") using the ternary.
  3. 8 min Write findLearner(id) using find. Call it with an id that exists and one that doesn't. Make the second case print "No learner with that id" instead of crashing.
  4. 8 min Show the learners' names on practice.html as a list, with createElement, textContent and replaceChildren.
  5. 8 min Break your own code on purpose three times (misspell a property, use an id that doesn't exist, break the import path). For each, write down the exact console message and the line it pointed to.
08

AI-assisted learning prompt

AI is a tutor today. A useful prompt once you have an error you don't understand:

Prompt

Here is a JavaScript error from my browser console: [paste the message]. Here is the line it points to: [paste the line]. Don't fix my code. Explain in plain words what the error means and what I should check, then ask me one question to test whether I understood.

Log it in the AI-use journal: the error, what the AI explained, and what you found when you checked.

09

Knowledge check

15 minutes
  1. What is the difference between what map, filter and find each return? Give one Tracker example of each.
  2. const learners = [] and then learners.push(...): is this allowed? Why, given that learners is const?
  3. Why is 2 === "2" false? Give one situation from a form where this would cause a bug.
  4. Why does a page that uses import fail when opened as a file:// address, and what fixes it?
  5. A console error says Cannot read properties of undefined (reading 'name'). List two things that could cause it in code using find.
  6. const status = rate < 70 ? "at risk" : "on track"; What is status when rate is 69, 70 and 71? Rewrite the line using if and else.
  7. Why does the lesson use textContent rather than innerHTML to show a learner's name, and why does it matter more from Day 7?
10

Build block: Learner Project Tracker

2 hours

Turn the Week 1 page from fixed text into a page driven by data.

  • Create data.js exporting a learners array (at least eight learners, made-up names only) and app.js importing it. Load app.js as a module and run the page through Live Server.
  • Replace the hard-coded learner content on the page with a list rendered from the array by JavaScript: create the list items in app.js from the data, using map and textContent, as in the core lesson.
  • Add one status calculation: show each learner's attendance rate, and mark anyone below 70% as "at risk" (a class you style in CSS). Include one learner with no sessions yet; they should show 0%, not "NaN%".
  • Add one lookup: a small labelled input and a button; when you type an id and click, the page shows that learner, or a clear "not found" message.
  • Commit on a branch, push, and merge into main through a pull request, as on Day 5. Check that the GitHub Pages link still works.
11

Flex hour: your three journals, then choosing a problem for P1

Do your three journals first (about 20 minutes). Then spend about 20 minutes on P1, and use any time left to catch up on the Build work.

What P1 is. From Week 3 to Week 6 you build your own product, P1. You pitch it on Pitch Day in Week 4 and defend it at Gate 6 for your Orange belt. If you want to keep building the Tracker, you can choose it as your P1.

Start with problems, not apps. Day 1 taught that a problem is described without any feature, screen or technology in it. So P1 doesn't start with "I want to build an app that…". It starts with a real problem you have seen people struggle with.

Why the problem's shape matters. Each of the coming weeks teaches one skill: storing data (Week 3), sign-in and access rules (Week 3), using it on a phone (Week 4), and an AI feature (Week 5). If your problem has the right shape, each of those weeks gives you something real to do on your own product. If it doesn't, you'll have nothing to practise on. So you choose a problem that already has that shape, and the features follow from it. You never bolt features on just to tick a box.

The four problem questions. Ask them about the problem, not about an app you imagine:

  1. Do the people involved keep track of many things over time? For example, orders, bookings, customers, stock, jobs or deliveries, written in a notebook, on WhatsApp or in someone's memory, where things get lost or forgotten. (If yes, solving it needs somewhere to store those things: Week 3.)
  2. Are there different kinds of people involved, who shouldn't all see or change the same things? For example, an owner and staff, or a business and its customers. A customer shouldn't see other customers' orders; staff shouldn't change prices. (If yes, solving it needs sign-in and rules about who can see what: Week 3.)
  3. Does at least one of those people meet the problem away from a desk? For example, a rider at a customer's door, an attendant at the counter, or a customer at home wanting to check on something. (If yes, solving it needs a phone: Week 4.)
  4. Is part of the problem about judgement or communication, not just counting? For example, writing reminders, summarising what happened this week, or deciding who to chase. Anything with no single correct answer. (If yes, that part is where AI can genuinely help, with a person checking the result: Week 5.)

Problems to avoid in this cohort. Some problems are real and worth solving, but not safely as a learner project. Choose a different problem if it is:

  • about children (for example, pupils' records), because the law requires extra protection for children's data;
  • about moving money (taking card payments, wallets, transfers). Keeping track of money is fine ("has this customer paid?", "who owes what?"); taking or moving it isn't;
  • about people's health (patient records, prescriptions), the most sensitive personal data there is;
  • impossible to explore without real people's private details, because you build and demonstrate on made-up data.

Your task (about 20 minutes). Write down three real problems you have seen people struggle with: family, friends, a shop or business you use. Describe each in two or three sentences with no app or feature in them. Then answer the four questions for each, with a short reason, and check the avoid-list. Mark the one that passes best. Tomorrow you'll talk to someone who has it.

12

Deliverables and submission criteria

Nothing is submitted today. Today, build section A (JavaScript and data) of your Tracker Frontend Pack, and start your P1 scope document. You hand both in for Quest 2 by Sunday 29 Nov, 11:59 pm WAT. Today's work should contain:

0 of 7 ticked Ticks are saved on this device only.

13

Daily journal reflection

Add today's entry to your three running journals (they're section E of your Frontend Pack):

  • Engineering journal: The error that took you longest today, what the console said, and how you found the cause. Estimate vs actual: for today's Tracker milestone, record your estimate, the actual time, and the reason for any big gap (being wrong is fine; hiding it isn't).
  • AI-use journal: Each time you asked your chat assistant something: the question, what it explained, and whether checking it yourself confirmed it.
  • Curriculum journal: Was writing all of today's code by hand the right level of difficulty? Were the four problem questions clear enough to apply to your own problems without help?