Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
javascript 1 exercises and projects completed
  • Loading branch information
mouzakiskir committed Feb 16, 2020
commit dc3c5422d8c12dc66a62e54b39ecbf5e03dcd6a7
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'
//You're at a party and you feel thirsty! However, you've got 5 friends who are also in need of a drink. Let's go get them a drink.

//Declare a variable that holds an empty array, called drinkTray.
let drinkTray=[];
let drinkTray=new Array(5);

//There are 3 different types of drinks:
//const drinkTypes = ["cola", "lemonade", "water"];
const drinkTypes = ["cola", "lemonade", "water"];
Expand All @@ -10,18 +11,11 @@ const drinkTypes = ["cola", "lemonade", "water"];
The drinkTray can only hold at most two instances of the same drink type, for example it can only hold 2 colas, 2 lemonades, 2 waters.
If there are already two instances of a drinkType then start with the next drink in the array.
Your drinkTray should contain 2 cola, 2 lemonade and 1 water.*/
let drinksNum = 0;

function checkDrinks (x){
return x == drinkTypes[drinksNum]
};

let x = 0;
for (let i = 0; i < 5; i++){
drinkTray.push(drinkTypes[drinksNum]);
if (drinkTray.filter(checkDrinks).length === 2){
drinksNum += 1;
}
drinkTray[i] = drinkTypes[x];
if(i%2 == 1) x++;
}

//Log to the console: "Hey guys, I brought a [INSERT VALUES FROM ARRAY]!" (For example: "Hey guys, I brought a cola, cola, lemonade, lemonade, water!")
console.log("Hey guys, I brought a " + drinkTray[0] + ", " + drinkTray[1] + ", " + drinkTray[2] + ", " + drinkTray[3] + ", " + drinkTray[4] + "!");
console.log("Hey guys, I brought a " + drinkTray.join()+".");
45 changes: 45 additions & 0 deletions Week2/homework/project_gradeCalc/gradeCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'
/*In this project you'll write a function that calculates grades,
based on the American grading system! Let's say a student did a test and they got a 60 out of 100, this function will:
1.convert the score into a percentage
2.calculate what grade corresponds with that percentage, and
3.shows in the command line the result: the grade and the percentage
In this example this is what we would expect the function to return in the command line:

You got a D (60%)!
When writing the function, make use of the following grade scores:

Grade A (90% - 100%)
Grade B (80% - 89%)
Grade C (70% - 79%)
Grade D (60% - 69%)
Grade E (50% - 59%)
Grade F (0% - 49%)*/



function grade(score){
if(score >= 90 && score <= 100){
console.log(`You got a A (${score}%)!`)
}
else if(score >= 80 && score <= 89){
console.log(`You got a B (${score}%)!`)
}
else if(score >= 70 && score <= 79){
console.log(`You got a C (${score}%)!`)
}
else if(score >= 60 && score <= 69){
console.log(`You got a D (${score}%)!`)
}
else if(score >= 50 && score <= 59){
console.log(`You got a E (${score}%)!`)
}
else if(score >= 0 && score <= 49){
console.log(`You got a F (${score}%)!`)
}
}

grade(Math.floor(Math.random() * 100));



61 changes: 61 additions & 0 deletions Week3/homework/credit card project/creditCardValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

function cardValidator(number) {
var number;
let even = number.match(/^(d*[0-9]){15}[02468]/gs); //Takes 15 digits and the last one must be even number, if not returns null.
let sameNum= number.match(/^(\d)(?!\1+$)\d{15}$/gs); //Checks if the numbers are the same, if not returns null.

function overSixteen() { //Function that checks if the sum off all numbers are over 16, if not returns null.
let sum = 0;
for (let i = 0; i < number.length; i++) {
sum += parseInt(number[i]);
}
if(sum<16){
return null;
}
}


if(even && sameNum && overSixteen != null) {
console.log("Valid card: "+number);
}
else
console.log("Invalid card: "+number)
}

cardValidator("1234567891234566");
cardValidator("6666666666661666");
cardValidator("a92332119c011112");
cardValidator("4444444444444444");
cardValidator("1111111111111110");
cardValidator("6666666666666661");


/*In this project you'll write a script that validates whether or not a credit card number is valid.

Here are the rules for a valid number:

- Number must be 16 digits, all of them must be numbers
- You must have at least two different digits represented (all of the digits cannot be the same)
- The final digit must be even
- The sum of all the digits must be greater than 16
- The following credit card numbers are valid:

9999777788880000
6666666666661666

The following credit card numbers are invalid:

a92332119c011112 (invalid characters)
4444444444444444 (only one type of number)
1111111111111110 (sum less than 16)
6666666666666661 (odd final number)

These are the requirements your project needs to fulfill:

- Make a JavaScript file with a name that describes its contents
- Create a function with a descriptive name, for example: `doSomething` or `calcAnotherThing`
- Write at least 2 comments that explain to others what a line of code is meant to do
- Make the return value of the function a template string, so you can insert variables!
- Use `node` from the command line to test if your code works as expected
*/
21 changes: 21 additions & 0 deletions Week3/homework/js-exercises/1_compliment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

//1. Write a function named `giveCompliment`
function giveCompliment(name){

/*- It takes 1 argument: your name
- Inside the function create an array with 10 strings. Each string should be a compliment, like `"great"`, `"awesome"`
- Write logic that randomly selects a compliment
- Return a string: "You are [COMPLIMENT], [YOUR_NAME]!*/

let compliment=["great","good","awesome","the best","excellent","superb","amazing","flawless","victorious","the man"];
const randomCompliment = compliment[Math.floor(Math.random() * compliment.length)];
return console.log(`You are ${randomCompliment},${name}!`)
}
//2. Call the function three times, giving each function call the same argument: your name.
for(let i=0; i<3; i++){
giveCompliment("Kiriako")
}



21 changes: 21 additions & 0 deletions Week3/homework/js-exercises/2_dogYears.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'
//You know how old your dog is in human years, but what about dog years? Calculate it!

/*1. Write a function named `calculateDogAge`.
- It takes 1 argument: your puppy's age (number).
- Calculate your dog's age based on the conversion rate of 1 human year to 7 dog years.
- Return a string: "Your doggie is [CALCULATED_VALUE] years old in dog years!"*/
function calculateDogAge(number){
let dogAge=number*7;

console.log(`Your doggie is ${dogAge} years old in dog years!`)
}

//2. Call the function three times with different sets of values.*/

for(let i=0; i<3; i++){
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
calculateDogAge(getRandomInt(15));
}
43 changes: 43 additions & 0 deletions Week3/homework/js-exercises/3_fortuneTeller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'
//Why pay a fortune teller when you can just program your fortune yourself?
/*1. Write a function named `tellFortune`.
- It takes 4 arguments: number of children (number), partner's name (string), geographic location (string), job title (string).
- Randomly select values from the arrays.
- Return a string: "You will be a [JOB_TITLE] in [LOCATION], and married to [PARTNER_NAME] with [NUMBER_KIDS] kids."
2. Create 4 arrays, `numChildren`, `partnerNames`, `locations` and `jobs`. Give each array 5 random values that make sense*/

let partnerNames=[
"Maria",
"Anna",
"Jeniffer",
"Penelope",
"Eleftheria"];

let locations=[
"Athens",
"New York",
"Tokyo",
"Berlin"];

let jobs=[
"doctor",
"lawer",
"police-officer",
"electrician",
"farmer"];

function tellFortune(randomJobs,randomLocations,randomPartner,numChildren){

numChildren=[getRandomInt(5)];
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
randomPartner = partnerNames[Math.floor(Math.random() * partnerNames.length)];
randomLocations = locations[Math.floor(Math.random() * locations.length)];
randomJobs = jobs[Math.floor(Math.random() * jobs.length)];

return(`You will be a ${randomJobs} in ${randomLocations}, and married to ${randomPartner} with ${numChildren} kids.`)
}

//3. Call the function 1 time, by passing the arrays as the argument.*/
console.log(tellFortune())
27 changes: 27 additions & 0 deletions Week3/homework/js-exercises/4_superMarket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'
/*Let's do some grocery shopping! We're going to get some things to cook dinner with. However, you like to spend your money and always buy too many things.
So when you have more than 3 items in your shopping cart the first item gets taken out.
1. Write a function named `addToShoppingCart`.
- It takes in 1 argument: a grocery item (string)
- Add grocery item to array. If the amount of items is more than 3 remove the first one in the array
- Return a string: "You bought [LIST_OF_GROCERY_ITEMS]!"
2. Create an array with 2 predefined strings: `"bananas"` and `"milk"`*/

let shoppingItems=[
"bananas",
"milk"
]
function addToShoppingCart(product){
if(shoppingItems.length > 2)
shoppingItems.shift()
shoppingItems.push(product)
return(`You bought ${shoppingItems}!`)
}

//3. Call the function 3 times, each time with a different string as the argument.
console.log(addToShoppingCart("bacon"));
console.log(addToShoppingCart("salmon"));
console.log(addToShoppingCart("beef"));



27 changes: 27 additions & 0 deletions Week3/homework/js-exercises/5_totalCost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'
/*You want to buy a couple of things from the supermarket to prepare for a party. After scanning all the items the cashier gives you the total price,
but the machine a broken! Let's write her a function that does it for her instead!

1. Write a function called `calculateTotalPrice`
- It takes 1 argument: an object that contains properties that only contain number values
- Add all the number values together
- Return a number: the total price of all items
2. Create an object named `cartForParty` with 5 properties. Each property should be a grocery item (like `beers` or `chips`) and hold a number value (like `1.75` or `0.99`)*/
function calculateTotalPrice(cartForParty){
cartForParty={
beer:1.75,
chips:0.99,
coca_cola:3.90,
pizza:6.70,
party_cake:15.70
}
let total = 0;
for (let property in cartForParty) {
total += cartForParty[property];
}
return total;
}
//3. Call the function 1 time, giving it the object `cartForParty` as an argument*/
console.log("Total price: "+calculateTotalPrice()+"€");