From 85745175726cf6820714844da327baf876c91de5 Mon Sep 17 00:00:00 2001 From: glenntyson Date: Tue, 31 Mar 2026 10:47:08 -0400 Subject: [PATCH 1/3] Glenntyson Javascript intro 2 --- src/glenntyson/js-data-types-lab/lab.js | 125 ++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/glenntyson/js-data-types-lab/lab.js diff --git a/src/glenntyson/js-data-types-lab/lab.js b/src/glenntyson/js-data-types-lab/lab.js new file mode 100644 index 0000000..750c45c --- /dev/null +++ b/src/glenntyson/js-data-types-lab/lab.js @@ -0,0 +1,125 @@ +function greet(name) { + return "Hello, " + name.toUpperCase() + "!"; +} +console.log(greet("Glenn")); + +// What does .length do? +// It returns the number of characters in a string (or number of elements in an array). + +// What does .includes() return? +// It returns true or false depending on whether a value exists inside a string or array. + +// What does .slice() do? +// It extracts part of a string or array and returns a new one without changing the original. + + +function checkEvenOdd(num) { + return num % 2 === 0 ? "Even" : "Odd"; +} +console.log(checkEvenOdd(4)); +console.log(checkEvenOdd(7)); + +// What does % do? +// The % operator (modulus) returns the remainder after division. + +// What does Math.random() return? +// It returns a random decimal number between 0 (inclusive) and 1 (exclusive). + +// When would you use Math.floor()? +// You use Math.floor() to round a number DOWN to the nearest whole number. +// Commonly used with Math.random() to get random integers. + +function sumArray(numbers) { + let total = 0; + + for (let i = 0; i < numbers.length; i++) { + total += numbers[i]; + } + + return total; +} +console.log(sumArray([1, 2, 3, 4])); + +// How do you access an array element? +// You use the index with square brackets. + + +// What does .push() do? +// It adds a new element to the END of an array. + +// Why do we use loops with arrays? +// To go through each element one by one so we can read, modify, or process all values. + + +function describePerson(person) { + return `${person.name} is ${person.age} years old`; +} + +// What is a key-value pair? +// It is a way to store data in objects where: +// "key" = the name (like a label) +// "value" = the data stored +// "name" is the key, "John" is the value + +// How do you access object data? +// You can use dot notation or bracket notation: +// person.name +// person["name"] + +// When would you use an object instead of an array? +// When you want to store related data with labels (keys), +// instead of just a list of values. + + + +// Step 1: Object +const student = { + name: "John", + scores: [80, 90, 75, 100] +}; + +// Step 2: Function to calculate average +function calculateAverage(scores) { + let total = 0; + + for (let i = 0; i < scores.length; i++) { + total += scores[i]; + } + + return total / scores.length; +} + +// Step 3: Function to determine grade +function getGrade(avg) { + if (avg >= 90) return "A"; + else if (avg >= 80) return "B"; + else if (avg >= 70) return "C"; + else if (avg >= 60) return "D"; + else return "F"; +} + +// Step 4: Run program +const average = calculateAverage(student.scores); +const grade = getGrade(average); + +// Step 5: Output +console.log(`Name: ${student.name}`); +console.log(`Average: ${Math.round(average)}`); +console.log(`Grade: ${grade}`); + + +// Reflection + +// Which data type felt easiest? +// Strings were easiest because they are just text. + +// Which one was most confusing? +// Objects were a little confusing because of keys and how to access them. + +// How do arrays and objects differ? +// Arrays are lists. +// Objects store data with labels (keys). + +// When would you use each in real applications? +// Arrays for lists (like scores). +// Objects for details (like a person). \ No newline at end of file From e7714ac3b5a2fd0287f18ea9625e0eb8c0bbdcfb Mon Sep 17 00:00:00 2001 From: glenntyson Date: Tue, 31 Mar 2026 11:58:32 -0400 Subject: [PATCH 2/3] glenn finish lab --- src/glenntyson/js-data-types-lab/lab.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/glenntyson/js-data-types-lab/lab.js b/src/glenntyson/js-data-types-lab/lab.js index 750c45c..7026a2a 100644 --- a/src/glenntyson/js-data-types-lab/lab.js +++ b/src/glenntyson/js-data-types-lab/lab.js @@ -119,6 +119,7 @@ console.log(`Grade: ${grade}`); // How do arrays and objects differ? // Arrays are lists. // Objects store data with labels (keys). +// // When would you use each in real applications? // Arrays for lists (like scores). From ce333e16b10a4b1663c700cdea0a1481a7354bff Mon Sep 17 00:00:00 2001 From: glenntyson Date: Tue, 31 Mar 2026 12:02:00 -0400 Subject: [PATCH 3/3] Glenn finished --- src/glenntyson/js-data-types-lab/lab.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/glenntyson/js-data-types-lab/lab.js b/src/glenntyson/js-data-types-lab/lab.js index 7026a2a..3356b48 100644 --- a/src/glenntyson/js-data-types-lab/lab.js +++ b/src/glenntyson/js-data-types-lab/lab.js @@ -118,7 +118,7 @@ console.log(`Grade: ${grade}`); // How do arrays and objects differ? // Arrays are lists. -// Objects store data with labels (keys). +// Objects store data with labels (keys).. // // When would you use each in real applications?