HCDD 411 — 20 Exercises covering everything from class
Lots of guidance
Good hints
Some hints
You're on your own
"Hello, World!" to the console.
console.log() to print a messageconsole.log("your text here")'...' or double quotes "..." around your text.
name and set it to any name (a string)age and set it to any numberisStudent and set it to true or falseconsole.log() to print all three variableslet variableName = value; — use let to declare your variables.
number and give it any valueif statement: if the number is greater than 0, print "Positive"else if: if the number is less than 0, print "Negative"else: otherwise print "Zero"> for "greater than" and < for "less than".
for loop to print the numbers 1 through 10, one per line.
for loop has three parts: for (start; condition; step)i = 1i <= 10i++console.log(i)for (let i = 1; i <= 10; i++) { ... }
add that takes two numbers and returns their sum. Then call it and print the result.
function add(a, b) { ... }return keyword to return a + badd(3, 5)return sends a value back. Without it, the function returns undefined.
student with properties: name, age, major, and gpastudent.namestudent["gpa"]let obj = { key: value, key2: value2 };
fruits with 3 fruit namespush() to add "mango" to the endunshift() to add "kiwi" to the beginningpop() to remove the last item — store it in a variable and print itpush() / unshift() return the new length. pop() / shift() return the removed item.
getGrade that takes a score parameter"A" for 90 and above"B" for 80–89"C" for 70–79"D" for 60–69"F" for anything below 60if / else if / else. Check from the highest score down so the conditions don't overlap.
while loop to count down from 10 to 1, then print "Blast off!".
count starting at 10while loop that runs as long as count > 0countcount by 1 each iteration using count--"Blast off!"count inside the loop — otherwise it will run forever!
forEach to iterate over an array of colors and print each one with its index number.
let colors = ["red", "green", "blue", "yellow"];colors.forEach() and pass a function with parameters (color, index)"0: red", "1: green", etc.`${index}: ${color}` or string concatenationforEach passes the element first, the index second: arr.forEach(function(element, index) { ... })
map() method to create a new array where every number is doubled.
let numbers = [1, 2, 3, 4, 5];numbers.map() to return a new array where each value is multiplied by 2doubled and print both the original and the new arraymap() takes a function that receives each element and returns the new value for that position.
filter() to extract only even numbers from an array.
let nums = [3, 8, 15, 4, 22, 7, 10, 13];nums.filter() to keep only the numbers where num % 2 === 0evens and print it% is the modulo operator. n % 2 === 0 is true when n is even.
rectangle with properties width and heightarea that returns width * heightperimeter that returns 2 * (width + height)this.width to refer to the object's own properties.
function square(n) { return n * n; } into an arrow functionfunction greet(name) { return "Hi, " + name + "!"; } into an arrow functionmap() with an arrow function to square each number in an arrayconst funcName = (param) => expression;. For a one-liner you can drop the curly braces and return keyword.
some() and every() to check conditions across an array of scores.
let scores = [72, 88, 95, 61, 83, 79];every() to check if ALL scores are passing (above 60) — print the boolean resultsome() to check if ANY score is an A (90 or above) — print the resultevery() to check if ALL scores are above 90 — print the resultevery() returns true only if the condition is true for all elements. some() returns true if at least one element satisfies the condition.
let data = [1, 3, 5, 7, 9, 2, 4, 6];.map() and .filter() together in a single expressionmap to double each number, then filter to keep only values > 10forEach to print a formatted summary for each student, and use filter to get only students with a GPA above 3.5.
forEach to print each student in the format: "Name: Alice | GPA: 3.8"filter to create a new array of students with GPA > 3.5forEachBankAccount class with a balance, and methods to deposit, withdraw, and check the balance.
BankAccount with a constructor that takes an initial balancedeposit(amount) method that increases the balancewithdraw(amount) method that decreases the balance — but only if there are enough funds. If not, print "Insufficient funds"getBalance() method that returns the current balancecountWords(words) that takes an array of stringsforEach to loop through the arrayRoster class that manages a list of students. It should support adding students, removing them by name, and computing the average GPA of the class.
Roster class should store an array of student objects internallyaddStudent(name, gpa) — push a new { name, gpa } object to the listremoveStudent(name) — filter out the student with the matching nameaverageGPA() — return the average GPA of all students (return 0 if the list is empty)printRoster() — use forEach to print every student's name and GPA