From 3a243741e3cb939e2336b4631c45162c3a02f2e3 Mon Sep 17 00:00:00 2001 From: han_T <72462953+HT88@users.noreply.github.com> Date: Sat, 28 Nov 2020 20:33:13 +0100 Subject: [PATCH] Add files via upload --- Week2/1_ removeCOMMA.js | 18 +++++++++++ Week2/2_eventOddREPORTER.js | 34 ++++++++++++++++++++ Week2/3_recipeCARD.js | 18 +++++++++++ Week2/4_readingLIST.js | 56 +++++++++++++++++++++++++++++++++ Week2/5_wannaDRINK.js | 62 +++++++++++++++++++++++++++++++++++++ Week2/6_gradeCALCULATOR.js | 30 ++++++++++++++++++ 6 files changed, 218 insertions(+) create mode 100644 Week2/1_ removeCOMMA.js create mode 100644 Week2/2_eventOddREPORTER.js create mode 100644 Week2/3_recipeCARD.js create mode 100644 Week2/4_readingLIST.js create mode 100644 Week2/5_wannaDRINK.js create mode 100644 Week2/6_gradeCALCULATOR.js diff --git a/Week2/1_ removeCOMMA.js b/Week2/1_ removeCOMMA.js new file mode 100644 index 000000000..cfd1fe77d --- /dev/null +++ b/Week2/1_ removeCOMMA.js @@ -0,0 +1,18 @@ + + +let myString = 'hello,this,is,a,difficult,to,read,sentence'; +console.log(myString.length); + +myString = myString.split(',').join(' '); +console.log(myString); + + +//Soluiton from shorturl.at/hyPV3 + + + + +//The method below works on arrays only??? :/ +//console.log(elements.join(' ')); +// expected output: "Fire-Air-Water" +//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join \ No newline at end of file diff --git a/Week2/2_eventOddREPORTER.js b/Week2/2_eventOddREPORTER.js new file mode 100644 index 000000000..c4d0e14fc --- /dev/null +++ b/Week2/2_eventOddREPORTER.js @@ -0,0 +1,34 @@ +'use strict' + +//let numberReporter = 20; + +for (let i = 1; i <= 20; i++) { + if (i % 2 == 0) { + console.log('The number is '+i+' even'); + } else { + console.log('The number is '+i +' odd'); +} +} + + + + + + +/* + +Things to think about: +- It does not work without 'let' +for (let i = 0; i < n; i++) { + console.log(i); +} + +SOURCES REFERENCED +https://www.w3resource.com/javascript-exercises/javascript-conditional-statements-and-loops-exercise-5.php + + +for (let i = 0; i <= 15; i++) { + console.log(i + ' is', (i % 2 == 0) ? "even" : "odd") +} + +*/ \ No newline at end of file diff --git a/Week2/3_recipeCARD.js b/Week2/3_recipeCARD.js new file mode 100644 index 000000000..756397a08 --- /dev/null +++ b/Week2/3_recipeCARD.js @@ -0,0 +1,18 @@ +'use strict' + +const myReceipts = { + Meal: 'Fried eggs', + Servings: 2, + Ingredients: ['Eggs (2x)', ' cooking oil', ' salt'] +}; + +// iterate over the user object +for (const key in myReceipts) { + console.log(`${key}: ${myReceipts[key]}`); +} + + +/* +Source: https://attacomsian.com/blog/javascript-iterate-objects + +*/ \ No newline at end of file diff --git a/Week2/4_readingLIST.js b/Week2/4_readingLIST.js new file mode 100644 index 000000000..7c356fc4f --- /dev/null +++ b/Week2/4_readingLIST.js @@ -0,0 +1,56 @@ +'use strict' + +const books = [ + { + title: 'Steppenwolf', + alreadyRead: true, + author: 'Hermann Hesse' + }, + { + title: 'Norwegian Wood', + alreadyRead: true, + author: 'Haruki Murakami' + }, + { + title: 'The Republic', + alreadyRead: false, + author: 'Plato' + } +]; + + +for(let i = 0; i < books.length; i = i + 1) { + const book = books[i]; + // you print the title + console.log(book.title + " by " + book.author); + // if you read it then say i read it + if (book.alreadyRead) { console.log("You already read " + book.title); + // if you didn't read it then say still need to read + } else { + console.log("You still need to read " + book.title); } + + // adds space in between the books. Let's give it a face lift! + console.log(""); +} + + + + +/*I still feel like this code is well above my capabilities. I will need to invest some more time into breakjng it down peice by piece. + +let booksRead = []; +for(let i = 0; i <= 2; i++) { + const book = books[i]; + let sentence = book.title + " by " + book.author; + console.log(sentence); + if (book.alreadyRead) { + booksRead.push("You already read " + book.title); + } else { + booksRead.push("You still need to read " + book.title); + } +} +booksRead.forEach(sentence => console.log(sentence)); + + + +*/ \ No newline at end of file diff --git a/Week2/5_wannaDRINK.js b/Week2/5_wannaDRINK.js new file mode 100644 index 000000000..cd91975f9 --- /dev/null +++ b/Week2/5_wannaDRINK.js @@ -0,0 +1,62 @@ +'use strict' + +const drinkTypes = ['cola', 'lemonade', 'water']; +const drinkTray = []; + +let drinkers = 10; + +for (let i = 0; i <= drinkers - 1; i = i + 1) { + const typeIndex = i % drinkTypes.length; + drinkTray[i] = drinkTypes[typeIndex];} + +console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`); + +//Completed with a little help from a friend. + + +/* +const drinkTypes = ['cola', 'lemonade', 'water']; +const drinkTray = []; + +// let i = 0; i =< 4;i++ +// let i = 0; we start from number 0 +// i =< 4; the condition, we stop the loop as soon as the condition is no longer TRUTHY; +// i++; incrementation; i += 1; i = i + 1; +function countOccurences(drink, drinkTray) { + let count = 0; + drinkTray.forEach(drinkOnTray => { + if (drinkOnTray === drink) { + count++; + } + }); + return count; +} + +// Fixed solution, solving the problem but only for the specific numbers of 5 +for(let i = 0; i <= 4;i++) { + if (i === 0 || i === 1) { + drinkTray.push(drinkTypes[0]); + } else if (i === 2 || i === 3) { + drinkTray.push(drinkTypes[1]); + } else { + drinkTray.push(drinkTypes[2]); + } +} + +console.log("IN DRINKTRAY", drinkTray); + +// Dynamic solution, that will work any amount of drinks and conditions +const numberPerDrink = 2; +const totalDrinks = 5; + +drinkTypes.forEach(drink => { + for(let i = 0; i <= 1; i++) { + if (drinkTray.length < totalDrinks) { + drinkTray.push(drink); + } + } +}) + +console.log("IN DRINKTRAY", drinkTray); + +*/ \ No newline at end of file diff --git a/Week2/6_gradeCALCULATOR.js b/Week2/6_gradeCALCULATOR.js new file mode 100644 index 000000000..5a033305a --- /dev/null +++ b/Week2/6_gradeCALCULATOR.js @@ -0,0 +1,30 @@ +'use strict' + +function yourGrade(pointsRECIEVED) { + +let grade = (pointsRECIEVED/100)*100; + +// Check if grade is an A, B, C, D, or F +if (grade >= 90) { + console.log("You got an A"); +} else if (grade >= 80) { + console.log("You got a B"); +} else if (grade >= 70) { + console.log("You got a C"); +} else if (grade >= 60) { + console.log("You got a D. Take it easey! D - stand for a degree!"); +} else { + console.log("F"); +} +} + +yourGrade(60); + + + + +/* +https://www.digitalocean.com/community/tutorials/how-to-write-conditional-statements-in-javascript + + +*/ \ No newline at end of file