From b827f74d3bd2a06dcb77196d49708721841168a0 Mon Sep 17 00:00:00 2001 From: Rod Sejas Date: Thu, 10 Mar 2022 10:33:16 +1100 Subject: [PATCH 1/2] arrays and functions --- .../3-wed/arrays/index.html | 11 ++ .../3-wed/arrays/js/homework.js | 180 ++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/index.html create mode 100644 Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/js/homework.js diff --git a/Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/index.html b/Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/index.html new file mode 100644 index 0000000..10c5fcd --- /dev/null +++ b/Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + +

Check the console.

+ + \ No newline at end of file diff --git a/Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/js/homework.js b/Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/js/homework.js new file mode 100644 index 0000000..a50bfba --- /dev/null +++ b/Rodrigo Sejas/wk01 - starts 7th Mar/3-wed/arrays/js/homework.js @@ -0,0 +1,180 @@ +// # Array and Functions Bonus Material + +// 1. Define a function `maxOfTwoNumbers` that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript. You'll have to remember your pre-work, or do some googling to figure this out. + +const maxOfTwoNumbers = function (a, b) { + if (a > b) { + return a; + } else { + return b; + } +}; + +// 2. Define a function `maxOfThree` that takes three numbers as arguments and returns the largest of them. + +const maxOfThree = function (a, b, c) { + if (a > b && a > c) { + return a; + } else if (b > a && b > c) { + return b; + } else { + return c; + } +}; + +const maxOfThree_two = function (a, b, c) { + return Math.max(a, b, c); +}; + +// if multiple numbers +function maxOfN(a, ...nums) { + // rest operator + winner = a; + nums.forEach((element) => { + if (element > winner) { + winner = element; + } + }); + return winner; +} + +// assign the first number as the 'winner' +// winner is the largest number +// compare against every number (loop through) +// if there is another number larger than our winner, reassign as winner +// return winner + +// + +// Examples +console.log(maxOfThree(10, 20, 30)); // Result = 30 +console.log(maxOfThree(11, 5, 10)); // Result = 11 +console.log(maxOfThree(1, 2, 2)); // Result = 2 + +// 3. Write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise. +// define function with param as char +function vowelCheck(char) { + let letter = char.toLowerCase(); + console.log(letter); + if (letter.includes("a")) { + return true; + } else if (letter.includes("e")) { + return true; + } else if (letter.includes("i")) { + return true; + } else if (letter.includes("o")) { + return true; + } else if (letter.includes("u")) { + return true; + } else { + return false; + } +} + +console.log(vowelCheck("i")); // Result = true +console.log(vowelCheck("U")); // Result = true +console.log(vowelCheck("p")); // Result = true +console.log(vowelCheck("V")); // Result = false +console.log(vowelCheck("A")); // Result = true + +// example of multiple - if (letter.includes('a') || letter.includes....) +// set a vowel array, use array.includes to search char + +// 4. Define a function `sumArray` and a function `multiplyArray` that sums and multiplies (respectively) all the numbers in an array of numbers. For example, `sumArray([1,2,3,4])` should return 10, and `multiplyArray([1,2,3,4])` should return 24. + +exampleArr = [1, 2, 3, 4]; + +function sumArray(arr) { + return arr.reduce((previousValue, currentValue) => { + return previousValue + currentValue; + }, 0); +} + +console.log(sumArray(exampleArr)); // Result = 10 + +function multiplyArray(arr) { + return arr.reduce((previousValue, currentValue) => { + return previousValue * currentValue; + }, 1); +} + +console.log(multiplyArray(exampleArr)); // Result = 24 + +// ## Bonus + +// 5. Define a function `reverseString` that computes the reversal of a string. For example, reverseString("jag testar") should return the string "ratset gaj". + +function reverseString(str) { + let newString = ""; + for (let i = str.length - 1; i >= 0; i--) { + newString += str[i]; // Need help with understanding this line! + } + return newString; +} + +console.log(reverseString("Hello")); + +// 6. Write a function `findLongestWord` that takes an array of words and returns the length of the longest one. + +// 7. Write a function `filterLongWords` that takes an array of words and an number `i` and returns the array of words that are longer than i. + +// # Homework: The Word Guesser + +// You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune). + +// - Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '\_', '\_', '\_' and end with 'F', 'O', 'X'). + +let word = ["F", "O", "X"]; +let guessedLetters = ["_", "_", "_"]; + +// - Write a function called guessLetter that will: +// - Take one argument, the guessed letter. + +function guess(guessLetter) { + guessLetter = guessLetter.toUpperCase(); + word.forEach(function check(letter, index) { + if (letter === guessLetter) { + guessedLetters[index] = letter; + console.log(guessedLetters.join("")); + console.log("Congrats motherfucker"); + if (unguessedLettersExist()) { + console.log("Double congrats motherfucker"); + } + } + }); +} + +// **ROD** forEach can return the index value, and forEach does nothing with returns +// **ROD** if the word had multiple of same letter, update the array to empty/replace the string after a correct guess (retains index) + +// - Iterate through the word letters and see if the guessed letter is in there. + +// - If the guessed letter matches a word letter, changed the guessed letters array to reflect that. + +// - When it's done iterating, it should log the current guessed letters ('F__') + +// and congratulate the user if they found a new letter. + +// - It should also figure out if there are any more letters that need to be guessed, + +const unguessedLettersExist = function () { + if (!guessedLetters.includes("_")) { + return true; + } +}; + +// and if not, it should congratulate the user for winning the game. + +// - Pretend you don't know the word, and call guessLetter multiple times with various letters to check that your program works. + +// ------- + +// ## Bonus: Make it more like Wheel of Fortune: +// - Start with a reward amount of $0 +// - Every time a letter is guessed, generate a random amount and reward the user if they found a letter (multiplying the reward if multiple letters found), otherwise subtract from their reward. +// - When they guess the word, log their final reward amount. + +// ## Bonus: Make it like Hangman: +// - Keep track of all the guessed letters (right and wrong) and only let the user guess a letter once. If they guess a letter twice, do nothing. +// - Keep track of the state of the hangman as a number (starting at 0), and subtract or add to that number every time they make a wrong guess. +// - Once the number reaches 6 (a reasonable number of body parts for a hangman), inform the user that they lost and show a hangman on the log. From 3c80221f684ecdc97075172d269f27dedcd53e54 Mon Sep 17 00:00:00 2001 From: Rodrigo Sejas Date: Fri, 11 Mar 2022 10:37:45 +1100 Subject: [PATCH 2/2] triangles --- .../wk01 - starts 7th Mar/4-thu/index.html | 11 + .../4-thu/js/homework.js | 223 ++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/index.html create mode 100644 Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/js/homework.js diff --git a/Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/index.html b/Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/index.html new file mode 100644 index 0000000..10c5fcd --- /dev/null +++ b/Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + +

Check the console.

+ + \ No newline at end of file diff --git a/Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/js/homework.js b/Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/js/homework.js new file mode 100644 index 0000000..80867ec --- /dev/null +++ b/Rodrigo Sejas/wk01 - starts 7th Mar/4-thu/js/homework.js @@ -0,0 +1,223 @@ +// # Geometry Function Lab + +// ### Part 1, Rectangle + +// Examples +const rectangleA = { + length: 5, + width: 5, +}; + +console.log(isSquare(rectangleA)); // Result = true + +const rectangleB = { + length: 6, + width: 5, +}; + +// Given the following a `rectangle` object like the one below, write the following functions which accept a `rectangle` object as an argument: + +// * isSquare - Returns whether the rectangle is a square or not + +function isSquare(obj) { + if (obj.length === obj.width) { + return true; + } else { + return false; + } +} + +console.log(isSquare(rectangleB)); // Result = false + +// * area - Returns the area of the rectangle + +function area(obj) { + let result = obj.length * obj.width; + return result; +} + +// * perimeter - Returns the perimeter of the rectangle + +function perimeter(obj) { + let result = 2 * (obj.length + obj.width); + return result; +} + +// ### Part 2, Triangle + +// Given the following a `triangle` object like the one below, write the following functions which accept a `triangle` object as an argument: + +// * isEquilateral - Returns whether the triangle is equilateral or not + +function isEquilateral(obj) { + if (obj.sideA === obj.sideB && obj.sideA === obj.sideC) { + return true; + } +} + +// * isIsosceles - Returns whether the triangle is isosceles or not + +function isIsosceles(obj) { + if ( + obj.sideA === obj.sideB || + obj.sideA === obj.sideC || + obj.sideC === obj.sideB + ) { + return true; + } +} + +// * area - Returns the area of the Triangle +function areaOfTriangle(obj) { + let a = obj.sideA; + let b = obj.sideB; + let c = obj.sideC; + let s = (a + b + c) / 2; + + let result = Math.sqrt(s * (s - a) * (s - b) * (s - c)); + return result.toFixed(2); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// * isObtuse - Returns whether the triangle is obtuse or not +// solve for all angles inside the triangle +// compare if any of the results is greater than 90 degrees + +const triangleA = { + sideA: 5, + sideB: 6, + sideC: 7, +}; + +let radians = Math.acos(19 / 35); +let angle = (radians * 180) / Math.PI; +console.log(angle); + +function getAngles(triangle) { + return [57, 40, 98]; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// # The Cash Register + +// Write a function called cashRegister that takes a shopping cart object. The function should return the total price of the shopping cart. + +function cashRegister(obj) { + // extract all prices using Object.values to an array + let pricesStr = Object.values(obj); + console.log(pricesStr); + // convert strings to numbers using .map + let pricesArr = pricesStr.map((i) => Number(i)); // Number wrap to manipulate str into numbers + console.log(pricesArr); + // reduce array with sum + let sum = pricesArr.reduce(function (previousValue, currentValue) { + return previousValue + currentValue; + }, 0); + return sum; +} + +// The object contains item names and prices (itemName: itemPrice). + +// // Input +const cartForParty = { + banana: "1.25", + handkerchief: ".99", + Tshirt: "25.01", + apple: "0.60", + nalgene: "10.34", + proteinShake: "22.36", +}; + +// // Output +cashRegister(cartForParty); // 60.55 + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// # Credit Card Validation + +// You're starting your own credit card business. You've come up with a new way to validate credit cards with a simple function called validateCreditCard that returns true or false. + +// Here are the rules for a valid number: + +// - Number must be 16 digits, all of them must be numbers +////// convert to str (removing the dash -) +////// check str length = + +function validateCreditCard(num) { + let newStr = num.split("-"); // use split method to divide into substrings in an array, using "-" as the seperator + newStr = newStr.join(""); // join the array into the var newStr + if (newStr.length !== 16) { + return false; + } + // all digits must be numbers + let newNum = Number(newStr); + if (Number.isNaN(newNum)) { + return false; + } + // - You must have at least two different digits represented (all of the digits cannot be the same) + + let valid = false; + for (let i = 0; i < newStr.length - 1; i++) { + if (newStr[i] !== newStr[i + 1]) { + valid = true; + break; + } + } + + if (valid === false) { + return false; + } + // - The final digit must be even + + let lastDigit = newStr[newStr.length - 1]; + lastDigit = Number(lastDigit); + if (lastDigit % 2 !== 0) { + return false; + } + let newArr = Array.from(newStr); + newArr = newArr.map(function (element) { + return Number(element); + }); + + // - The sum of all the digits must be greater than 16 it is valid + + let sum = newArr.reduce(function (previousValue, currentValue) { + return previousValue + currentValue; + }, 0); + + if (sum <= 16) { + return false; + } + + return true; +} + +// The following credit card numbers are valid: + +// - `9999-9999-8888-0000` // result true +// - `6666-6666-6666-1666` // result true + +// The following credit card numbers are invalid: + +// - `a923-3211-9c01-1112` invalid characters // false +// - `4444-4444-4444-4444` only one type of number +// - `1111-1111-1111-1110` sum less than 16 +// - `6666-6666-6666-6661` odd final number + +// ## Example + +validateCreditCard("9999-9999-8888-0000"); // Returns: true + +// *Hint*: Remove the dashed from the input string before checking if the input credit card number is valid. + +// *Bonus*: Return an object indicating whether the credit card is valid, and if not, what the error is + +// ``` +// { valid: true, number: 'a923-3211-9c01-1112' } +// { valid: false, number: 'a923-3211-9c01-1112', error: ‘wrong_length’ } +// ``` + +// *Double Bonus*: Make your credit card scheme even more advanced! What are the rules, and what are some numbers that pass or +// fail? Ideas: check expiration date! Check out the Luhn Algorithm for inspiration;