TalentJR

🟨 JavaScript tutorial for beginners · Class 9 · Lesson 6 of 18 · Free

Full Projects – Plan and Build Complete JavaScript Apps

⏱ About 45 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Plan a project as data, actions and screen before writing code
  • Build a to-do app with a class, a render function, events and localStorage
  • Build a weather app with fetch, a loading message and friendly errors, and test your code

You now know classes, array methods, Promises, fetch and error handling. On their own they are like separate cricket skills — batting, bowling, fielding. A match needs all of them together. In this lesson you join them into two real apps: a to-do list that remembers your tasks, and a weather app that talks to a server.

How do I plan a JavaScript project before I start coding?

Big projects fail when you start typing straight away. First answer three questions. Data: what do I need to remember? Actions: what can the user do? Screen: what should they see?

For a to-do app: the data is a list of tasks, each with an id, a title and done (true or false). The actions are add, tick and delete.

Build and test the data part in the console first. If the data is right, drawing it on the page is the easy part.

🎬 How JavaScript thinks — step 1 of 13JavaScript is about to run line 1
▶const tasks = [];
2let nextId = 1;
3
4function addTask(title) {
5 tasks.push({ id: nextId, title, done: false });
6 nextId = nextId + 1;
7}
8function toggleTask(id) {
9 const task = tasks.find((t) => t.id === id);
10 task.done = !task.done;
11}
12addTask("Maths homework");
13addTask("Pack school bag");
14toggleTask(1);
15console.log(tasks);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
[{ id: 1, title: "Maths homework", done: true }, { id: 2, title: "Pack school bag", done: false }]

💡 { id: nextId, title, done: false } — writing just title means title: title.

How do I organise project code with a JavaScript class?

Loose functions and variables get messy as a project grows. A class (Lesson 1) keeps the data and all its actions in one place.

Each method does one small job. `remove` uses `filter` and `summary` uses `reduce` from Lesson 2.

Methods also guard the data: `add` throws an error for an empty title (Lesson 5), so a blank task can never sneak in.

🟨 JavaScript — you can edit this code
Console output
["Revise Science", "Water the tulsi plant"]
1 of 2 done

💡 this.nextId++ uses the current id, then adds 1 to it for next time.

How do I build a to-do list app with JavaScript?

Now the screen. The rule from Lesson 2 still holds: change the data, then call `render()` to redraw everything from the data.

Type a task and press Add. Click a task to tick it, and click ✖ to delete it.

We use one click listener on the whole list instead of one per item. `event.target.dataset` reads the data-id or data-del attribute of whatever was clicked, so we know which task and which action.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
• Finish maths homework

💡 Real apps should not put user text straight into innerHTML; for your own practice page it is fine. Use textContent when showing text from strangers.

How do I save project data with localStorage in JavaScript?

A to-do list that forgets everything on refresh is not much use. In Class 8 Lesson 13 you met `localStorage`. It keeps text in the browser, even after you close the tab.

It can only store text, so save with `JSON.stringify` and load with `JSON.parse` (Lesson 4).

The first time, nothing is saved yet and `getItem` gives `null`. Saved text can also be broken. So load inside try/catch and fall back to an empty list.

🎬 How JavaScript thinks — step 1 of 14JavaScript is about to run line 1
▶const KEY = "tj-tasks";
2
3function save(tasks) {
4 localStorage.setItem(KEY, JSON.stringify(tasks));
5}
6function load() {
7 try {
8 return JSON.parse(localStorage.getItem(KEY)) || [];
9 } catch (err) {
10 console.log("Saved data was broken, starting fresh");
11 return [];
12 }
13}
14save([{ id: 1, title: "Buy diyas", done: false }]);
15const tasks = load();
16tasks.push({ id: 2, title: "Make rangoli", done: true });
17save(tasks);
18console.log(load());
19console.log(typeof localStorage.getItem(KEY));
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
[{ id: 1, title: "Buy diyas", done: false }, { id: 2, title: "Make rangoli", done: true }]
string

💡 || [] means: if the left side is null (nothing saved), use an empty array instead.

How do I build a weather app with JavaScript and an API?

The second project joins fetch (Lesson 4) with error handling (Lesson 5). We use the TalentJR practice API, whose weather is made up for learning.

A good app shows three states: loading, success and error. Try Goa to see the error state — it is not in the practice data.

Notice `finally` turns the button back on whatever happens, so the user can try again.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
Pune: 24°C | Mon 29° · Tue 30° · Wed 28°
😕 City not found. Try Mumbai or Delhi.

💡 encodeURIComponent makes any city name safe to put inside a URL.

How do I test my JavaScript project before I share it?

Clicking around by hand misses things. Write a tiny `check` function that compares what you got with what you expected, and prints ✅ or ❌.

Test the data class from section 2, including the error cases. If every line is ✅, you can change code later without fear. (Lesson 17 goes deeper into testing.)

Before sharing, also check: clear names, no leftover test buttons, and a short note on how to use the app.

🟨 JavaScript — you can edit this code
Console output
✅ adds one task
✅ trims spaces
✅ starts not done
✅ empty title throws

💡 Try it: change "Read a story" in the second check to "Read a book" and watch a ❌ appear.

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

localStorage.setItem("score", 50);
const saved = localStorage.getItem("score");
console.log(saved + 10);

Question 2. What does this print?

let tasks = [
  { id: 1, title: "Maths" },
  { id: 2, title: "Science" },
  { id: 3, title: "English" }
];
tasks = tasks.filter((t) => t.id !== 2);
console.log(tasks.map((t) => t.title));

Question 3. What does this print?

const task = { title: "Feed the cat", done: false };
task.done = !task.done;
task.done = !task.done;
task.done = !task.done;
console.log(task.done);

🧩 Your challenge

Build a pocket-money expense tracker page. The form has a name box, an amount box and an Add button. Keep the expenses in an array of { name, amount }. On every add: check the name is not empty and the amount is a number above 0 (otherwise show the error in #msg), push the expense, and call render(). render() shows each expense as a list item "name – ₹amount" and shows "Total: ₹…" in #total using reduce, and also console.logs the total text. The starter adds two expenses through the same function on load. Expected console output: Total: ₹40 Total: ₹160

Expected output in the console:
Total: ₹40
Total: ₹160
🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it

⚠️ Common mistakes (and how to fix them)

❌ Wrong

const tasks = ["Maths", "Science"];
const list = document.querySelector("#list");
function render() {
  for (const t of tasks) {
    list.innerHTML += `<li>${t}</li>`;
  }
  console.log("Items on page:", list.children.length);
}
render();
tasks.push("English");
render();

No error, but the second render shows 5 items instead of 3 — Maths and Science appear twice.

✅ Right

const tasks = ["Maths", "Science"];
const list = document.querySelector("#list");
function render() {
  list.innerHTML = "";
  for (const t of tasks) {
    list.innerHTML += `<li>${t}</li>`;
  }
  console.log("Items on page:", list.children.length);
}
render();
tasks.push("English");
render();

render must redraw from scratch. Clear the list first, or build the whole HTML with map and join and set it once.

❌ Wrong

const tasks = JSON.parse(localStorage.getItem("my-new-tasks"));
tasks.push("Learn JavaScript");
console.log(tasks);

TypeError: Cannot read properties of null (reading 'push')

✅ Right

const tasks = JSON.parse(localStorage.getItem("my-new-tasks")) || [];
tasks.push("Learn JavaScript");
console.log(tasks);

The very first time, nothing is saved, so getItem returns null and JSON.parse(null) is null. Fall back to an empty array with || [].

❌ Wrong

const button = document.querySelector("add");
button.addEventListener("click", () => console.log("Added!"));
console.log("Ready");

TypeError: Cannot read properties of null (reading 'addEventListener')

✅ Right

const button = document.querySelector("#add");
button.addEventListener("click", () => console.log("Added!"));
console.log("Ready");

querySelector needs # for an id. Without it, it looks for an <add> tag, finds nothing, and returns null.

📌 Remember

  • Plan first: data (what to remember), actions (what users do), screen (what they see).
  • Build and test the data part in the console, then connect it to the page.
  • Keep data in one place (an array or a class) and redraw with render() after every change.
  • Save with JSON.stringify into localStorage; load with JSON.parse and a || [] fallback.
  • Apps that fetch data should show loading, success and error states.
  • A tiny check() function catches bugs before your users do.

Finished the quiz and the challenge?

Questions students ask

What JavaScript projects can a Class 9 student build?

Good first projects are a to-do list, an expense tracker, a quiz app, a weather app using a practice API, a marks calculator, or a simple game like rock-paper-scissors. Pick something you or your family would actually use.

How do I start a JavaScript project when I don't know where to begin?

Write down the data, the actions and the screen on paper. Then build the smallest working piece, like adding one task, and test it in the console. Add one feature at a time and run your code after each one.

Where can I put my finished project so others can see it?

A project made of HTML, CSS and JavaScript files can be put online for free on static hosting services, with a parent's or teacher's help to set up an account. Ask them before sharing anything online.

Why does my data disappear when I refresh the page?

Variables live only while the page is open. To keep data, save it with localStorage.setItem after each change and load it with localStorage.getItem when the page starts.