Javascript - Objects
Learning goals
Teacher instruction
<div class="left"> <a> <img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/TV_2_%282023%29.svg/1200px-TV_2_%282023%29.svg.png" alt="tv2Logo"> </a> <a>Nyheder</a> <a>Sport</a> <a>Vejr</a> <a>TV</a> </div>.NobelPicture { width: 40rem; height: 20rem;function getStartupName() { const firstWordRandom = Math.floor(Math.random() * firstWords.length); const secondWordsRandom = Math.floor(Math.random() * secondWords.length); const firstWordForCombine = firstWords [firstWordRandom]; const secondWordForCombine = secondWords [secondWordsRandom]; const startupName = `${firstWordForCombine} ${secondWordForCombine}` return `${startupName}`; }const first = firstWords[Math.floor(Math.random() * firstWords.length)];function rollDice() { // Generates a random number between 1 and 6 return Math.floor(Math.random() * 6) + 1; }<!-- Videos --> <section class="videos"> <div class="video"> <video height="225" width="400" controls poster="https://i.ytimg.com/vi/hEHJuHlI7bA/hq720.jpg?sqp=-oaymwEnCNAFEJQDSFryq4qpAxkIARUAAIhCGAHYAQHiAQoIGBACGAY4AUAB&rs=AOn4CLCIxWvGbTu03AYIlJlrZvyxMlOfKw" src="https://www.youtube.com/embed/il_hEHJuHlI7bA?autoplay=1&mute=1" class="video-one"> </video> <div class="video-description"> <img height="40" width="40" src="images/wirual-logo.jpg" alt="Wirtual logo" class="wirtual-logo"> <div class="video-text"> <p class="caption">These Weekly Campaign Records are a Work of Art.</p> <p class="creator">WirtualTV</p> </div> </div> </div> <div class="video"> <video height="225" width="400" controls poster="https://i.ytimg.com/vi/hEHJuHlI7bA/hq720.jpg?sqp=-oaymwEnCNAFEJQDSFryq4qpAxkIARUAAIhCGAHYAQHiAQoIGBACGAY4AUAB&rs=AOn4CLCIxWvGbTu03AYIlJlrZvyxMlOfKw" src="https://www.youtube.com/embed/il_hEHJuHlI7bA?autoplay=1&mute=1" class="video-one"> </video> <div class="video-description"> <img height="40" width="40" src="images/wirual-logo.jpg" alt="Wirual logo" class="wirtual-logo"> <div class="video-text"> <p class="caption">These Weekly Campaign Records are a Work of Art.</p> <p class="creator">WirtualTV</p> </div> </div> </div>//if statement to display how well the class has done based on their average grade if (getAverage(grades) >= 90) { console.log("Overall Class Performance: Excellent"); } else if (getAverage(grades) >= 80 && getAverage(grades) <= 89) { console.log("Overall Class Performance: Good"); } else if (getAverage(grades) >= 70 && getAverage(grades) <= 79) { console.log("Overall Class Performance: Satisfactory"); } else if (getAverage(grades) < 70) { console.log("Overall Class Performance: Needs improvement"); } else { console.log("ERROR"); }// actual game function function PlayGame(){ if (confirm("Do you want to start a dice tournament?")===false){ return } let gameEnded = false amountOfPlayers = prompt("How many players? (max 8)") while (true){ // prompt proofing if (amountOfPlayers === ""){ amountOfPlayers = prompt("Error: please input a number!\nHow many players? (max 8)") } else if (isNaN(amountOfPlayers)){ amountOfPlayers = prompt("Error: please input a number!\nHow many players? (max 8)") }else if (amountOfPlayers>8){ amountOfPlayers = prompt("Error: no more than 8 players!\nHow many players? (max 8)") } else{ break } } for (let i = 0; i < amountOfPlayers; i++) { let playerName = prompt(`Player ${i+1} name: \n(leave blank for name: Player ${i+1})`) if (playerName === "") { players.push([`Player ${i+1}`, [], 0]) }else{ players.push([playerName, [], 0]) } } if (confirm("Do you want to play standart settings?\n[best of 3 sets] of [10 rounds]\n\n(Press cancel to change settings)")===false){ sets = prompt("How many sets?") rounds = prompt("How many rounds?") while (true){ // prompt proofing if (isNaN(sets)){ sets = prompt("Error: please input a number!\nHow many sets?") } else if (sets>10){ sets = prompt("Error: no more than 10 sets!\nHow many sets?") }else if (isNaN(rounds)){ rounds = prompt("Error: please input a number!\nHow many rounds?") }else if (rounds>20){ rounds = prompt("Error: no more than 20 rounds!\nHow many rounds?") } else{ break } } } // actual game loop for (let set = 1; set <= sets; set++) { for (let round = 1; round <= rounds; round++) { let roundMessage = "" if (sets > 1) { roundMessage = `Set: ${set} (best of ${sets})\n` } roundMessage += `Round: ${round}/${rounds}\n` for (const player of players) { if (round === 1) { player[1].push([]) } let playerScore = 0 alert(roundMessage + `${player[0]}, press Enter to roll...`) let roll = DiceRoll() player[1][set - 1].push(roll) // get players total score this set for (const setScore of player[1][set - 1]) { playerScore += setScore } let rollMessage = "" if (sets > 1) { // show sets if more sets are played rollMessage += `${player[0]} rolled: 🎲 ${roll} 🎲 (Total: ${playerScore}, Sets: ${player[2]})\n` } else { rollMessage += `${player[0]} rolled: 🎲 ${roll} 🎲 (Total: ${playerScore})\n` } roundMessage += rollMessage } if (round === rounds) { // if last round was played alert(roundMessage + "\n-------------------------\n----- Set finished! -----\n-------------------------") } else { alert(roundMessage + "\n--- Round finished ---") } } // 2 arrays: one which is sorted, and one that isn't, used to reference original player index const scoreboard = [] let sortedScoreboard = [] for (const player of players) { let setTotal = 0 for (const setScores of player[1][set - 1]) { setTotal += setScores } player[1][set - 1].push(setTotal) scoreboard.push([player[0], setTotal]) sortedScoreboard.push([player[0], setTotal]) } sortedScoreboard.sort((a, b) => b[1] - a[1]) // sort players by set total let winningIndex = 0 // if top 2 players are tied, play sudden death if (sortedScoreboard[0][1] === sortedScoreboard[1][1]) { let player1won = SuddenDeath([sortedScoreboard[0][0], sortedScoreboard[1][0]]) if (player1won === false) { winningIndex = 1 } } // find which player is first to update their score. for (let i = 0; i < scoreboard.length; i++) { if (scoreboard[i][0] === sortedScoreboard[winningIndex][0]) { players[i][2] += 1 // if the winner has a lead to secure a ´best of x´ win, end tournament if (players[i][2] > (sets / 2)) { GameOver(true) gameEnded = true break } else { alert(`Set winner is: ${sortedScoreboard[winningIndex][0]}`) } } } if (gameEnded){ break } } if (gameEnded === false){ GameOver() // end game after all sets are played } }
Peer instruction 1 min
Question 1
Question 2
Flipped classroom videos
What is an object?
What problem does obejcts solve?
Objects
Accessing data using key
Changing data using key
Array of objects
Exercises
📝 Exercise 1 - level 1
📝 Exercise 2 - level 1
📝 Exercise 3 - level 1
📝 Exercise 4 - level 1
📝 Exercise 5 - level 2
📝 Exercise 6 - Lets analyze this code in class
📝 Exercise 7 - Please improve the following code
8 - Build a sentiment analyzer
9 - Character frequencies - optional
10 - Credit card number formatter - optional

Algebraic notation chess svær opgave
Last updated