Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
19 lines (19 loc) · 4.94 KB
/
Copy pathscript.js
File metadata and controls
19 lines (19 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"use strict";
const $=s=>document.querySelector(s);
const lessons=[
{title:"Hello, Java!",level:"Beginner",text:"Java is a statically typed programming language. The main method is the entry point of a basic application.",code:'public class Main {\n public static void main(String[] args) {\n System.out.println("Hello, Java!");\n }\n}',question:"What prints text to the console?",choices:["System.out.println()","console.log()","print()"],correct:0},
{title:"Variables & Types",level:"Beginner",text:"Use int for whole numbers, double for decimals, boolean for true or false, and String for text.",code:'int age = 15;\ndouble score = 9.5;\nboolean active = true;\nString name = "Alex";',question:"Which type stores a true or false value?",choices:["String","boolean","int"],correct:1},
{title:"Operators",level:"Beginner",text:"Operators perform calculations and comparisons. Use == to compare primitive values and = for assignment.",code:'int x = 12;\nint y = 5;\nSystem.out.println(x + y);\nSystem.out.println(x > y);',question:"Which operator means greater than?",choices:[">","==","="],correct:0},
{title:"Conditionals",level:"Beginner",text:"Use if, else if, and else to make decisions based on boolean conditions.",code:'int score = 85;\nif (score >= 60) {\n System.out.println("Pass");\n} else {\n System.out.println("Try again");\n}',question:"What runs when an if condition is false?",choices:["The if block","The else block","The import statement"],correct:1},
{title:"For Loops",level:"Beginner",text:"Use for loops when you know how many repetitions are needed.",code:'for (int i = 1; i <= 5; i++) {\n System.out.println(i);\n}',question:"How many numbers are printed?",choices:["4","5","6"],correct:1},
{title:"While Loops",level:"Beginner",text:"A while loop repeats until its condition becomes false. Remember to update the variables used in its condition.",code:'int count = 3;\nwhile (count > 0) {\n System.out.println(count);\n count--;\n}',question:"What is printed first?",choices:["1","2","3"],correct:2},
{title:"Methods",level:"Intermediate",text:"Methods organize reusable functionality and can return values.",code:'static int square(int value) {\n return value * value;\n}\n// square(4) returns 16',question:"What does square(4) return?",choices:["8","16","4"],correct:1},
{title:"Arrays",level:"Intermediate",text:"Arrays hold a fixed number of elements of the same type and use zero-based indexes.",code:'String[] fruits = {"Apple", "Pear"};\nSystem.out.println(fruits[0]);',question:"What is fruits[0]?",choices:["Pear","Apple","0"],correct:1},
{title:"Classes & Objects",level:"Intermediate",text:"Classes define fields and methods. Objects are created from classes with the new keyword.",code:'class Cat {\n String name;\n Cat(String name) { this.name = name; }\n}\nCat cat = new Cat("Milo");',question:"Which keyword creates an object?",choices:["object","class","new"],correct:2},
{title:"ArrayList",level:"Intermediate",text:"An ArrayList is a resizable collection, unlike fixed-size arrays.",code:'import java.util.ArrayList;\nArrayList<String> names = new ArrayList<>();\nnames.add("Sam");\nSystem.out.println(names.size());',question:"Which method adds an item?",choices:["add()","append()","insert()"],correct:0}
];
let selected=0,finished=[];try{const saved=JSON.parse(localStorage.getItem("javaMasteryProgress")||"[]");if(Array.isArray(saved))finished=saved.filter(n=>Number.isInteger(n)&&n>=0&&n<lessons.length)}catch(e){}
function save(){localStorage.setItem("javaMasteryProgress",JSON.stringify(finished))}
function render(){const data=lessons[selected];$("#heading").textContent=data.title;$("#level").textContent=data.level;$("#explanation").textContent=data.text;$("#code").textContent=data.code;$("#question").textContent=data.question;$("#result").textContent="";$("#choices").replaceChildren();data.choices.forEach((choice,index)=>{const button=document.createElement("button");button.className="btn secondary";button.textContent=String.fromCharCode(65+index)+". "+choice;button.onclick=()=>{const ok=index===data.correct;$("#result").className=ok?"success":"error";$("#result").textContent=ok?"Correct! Your progress has been saved.":"Not quite. Read the lesson and try again.";if(ok&&!finished.includes(selected)){finished.push(selected);save();renderMenu()}};$("#choices").append(button)});renderMenu()}
function renderMenu(){$("#lessonList").replaceChildren();lessons.forEach((l,index)=>{const b=document.createElement("button");b.className="btn "+(selected===index?"":"secondary");b.textContent=(finished.includes(index)?"✓ ":"")+(index+1)+". "+l.title;b.onclick=()=>{selected=index;render()};$("#lessonList").append(b)});$("#completed").textContent=finished.length;$("#progress").textContent=Math.round(finished.length/lessons.length*100)+"%"}
$("#copy").onclick=()=>{navigator.clipboard.writeText(lessons[selected].code).then(()=>{$("#copy").textContent="Copied";setTimeout(()=>$("#copy").textContent="Copy code",1500)}).catch(()=>$("#copy").textContent="Select code to copy")};render();