Skip to content

Commit 9bde813

Browse files
author
dot.not
committed
Commit Homework week 3
1 parent c7e40f3 commit 9bde813

9 files changed

Lines changed: 289 additions & 0 deletions

File tree

Week3/homework/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<title id="id_page_title"></title>
6+
<meta charset="utf-8" />
7+
<link rel="stylesheet" type="text/css" href="./main.css" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
9+
</head>
10+
<body id="id_page_container">
11+
<p id="id_page_caption"
12+
class="cls_text_align_right cls_background_color_gold"
13+
style="padding:10px 20px">
14+
</p>
15+
<div class="cls_text_align_left cls_background_color_gold">
16+
Open the console to see the results
17+
</div>
18+
<script src="./main.js"></script>
19+
<script src="./js-exercises/ex01-compliment.js"></script>
20+
<script src="./js-exercises/ex02-dog-years.js"></script>
21+
<script src="./js-exercises/ex03-fortune-teller.js"></script>
22+
<script src="./js-exercises/ex04-shopping-cart.js"></script>
23+
<script src="./js-exercises/ex05-total-price.js"></script>
24+
<script src="./js-exercises/project-ccn-validate.js"></script>
25+
</body>
26+
</html>
27+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
/*
4+
description exercise with accessing a random array element
5+
*/
6+
7+
function runExercise01 () {
8+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
9+
console.log("JS-1 - week 3 - Exercise 01 - Random compliment");
10+
console.log("Access random array element");
11+
console.log("- - - - - - - - - - - - - - - - - - - -");
12+
function getCompliment(myName){
13+
let listCompliments=[ 'Awesome', 'Magnificent', 'Amazing', 'Wondeful', 'Wondrous',
14+
'Mind-blowing', 'Jaw-dropping', 'Excellent', 'Marvelous', 'Impressive' ];
15+
return `Hey ${myName}, you are ${listCompliments[Math.floor(Math.random()*10)]}!`;
16+
}
17+
console.log(getCompliment("Manolis"));
18+
console.log(getCompliment("Manolis"));
19+
console.log(getCompliment("Manolis"));
20+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
21+
console.log(" ");
22+
};
23+
24+
runExercise01();
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
/*
4+
description exercise with calculating a number based on a rule
5+
*/
6+
7+
function runExercise01 () {
8+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
9+
console.log("JS-1 - week 3 - Exercise 02 - Dog years");
10+
console.log("Calculate a number based on rules");
11+
console.log("- - - - - - - - - - - - - - - - - - - -");
12+
function calculateDogAge(dogName,isMale,yearBorn){
13+
let currentYear=(new Date()).getFullYear();
14+
// normalize dog's birthYear - allowed values from (currentYear-20) to currentYear
15+
yearBorn=(yearBorn<(currentYear-20)?currentYear-20:
16+
(yearBorn>currentYear?currentYear:yearBorn));
17+
let bornText=(yearBorn==currentYear?"this year":
18+
(yearBorn==(currentYear-1)?"last year":`in ${yearBorn}`));
19+
let yearText=(currentYear-yearBorn)==0?"a newborn pup":
20+
`approximately ${(currentYear-yearBorn)*7} dog years old!`;
21+
return `My dog ${dogName} was born ${bornText}, ${isMale?"he":"she"} is ${yearText}`;
22+
}
23+
console.log(calculateDogAge("Bleu",false,1915));
24+
console.log(calculateDogAge("Ruby",false,2020));
25+
console.log(calculateDogAge("Max",true,2019));
26+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
27+
console.log(" ");
28+
};
29+
30+
runExercise01();
31+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
/*
4+
description exercise with array parameters & rng
5+
*/
6+
7+
function runExercise03 () {
8+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
9+
console.log("JS-1 - week 3 - Exercise 03 - Fortune teller");
10+
console.log("Array parameters & random element selection");
11+
console.log("- - - - - - - - - - - - - - - - - - - -");
12+
function tellFortune(listJobs,listCities,listSpouceNames,listChildren){
13+
function spliceRandomValue(anArray){
14+
return anArray.splice(Math.floor(Math.random()*anArray.length),1)};
15+
function addPrefix(paramValue){
16+
return ('aeioy'.indexOf(paramValue.toString()[0].toLowerCase())<0?"a ":"an ")+paramValue};
17+
let childNum=spliceRandomValue(listChildren);
18+
let childrenText=(childNum<1?"without children":"with "
19+
+(childNum==1?"one child":`${childNum} kids`));
20+
return `You will be ${addPrefix(spliceRandomValue(listJobs))}`
21+
+` living in ${spliceRandomValue(listCities)}`
22+
+`, married to ${addPrefix(spliceRandomValue(listJobs))}`
23+
+` named ${spliceRandomValue(listSpouceNames)} ${childrenText}.`;
24+
}
25+
let funnyJobsList=['Chocolate Beer Wrestler Administrator','Cat Editor Behavior Consultant'
26+
,'Unicorn Division Chief Farmer','Belly Dance Ninja Poet'
27+
,'Bride Kidnapping Master Carpenter','Web Snake Developer-Milker'
28+
,'Sub-Commitment Under-Secretary','Master Handshake Broker'
29+
,'Water Slide Toast Tester','Professional Sleeper Pilot'
30+
,'Inspiration Office Rider','Beverage Dissemination Officer Agent'
31+
,'Digital Prophet Boxer','Galactic Research Viceroy of Excellence'
32+
,'Space Travel Agent-Burglar','Chief Troublemaker Commando'
33+
,'In-house Zoologist-Philosopher','Shadow Accounting Dancer'
34+
,'Electrician-Gynecologist','Computer Speech Accountant'
35+
,'Occupational Hazard Engineer','Hunter-Seeker Epidemiologist'];
36+
let locationNames=['Dublin','Bruges','Porto','Berlin','Zagre','Marseille','Stockholm'
37+
,'Athens','Oslo','Bratislava','Frankfurt','Amsterdam'];
38+
let partnerNames=['Luisa','Gema','Sandra','Alba','Ariadne','Rosa'
39+
,'Rebeca','Sonia','Kate','Nastasje','Eva'];
40+
let numChildren=[0,1,2,3,0,1,2,3,0,1,2,1];
41+
for (let i=0; i<10; i++)
42+
{console.log(tellFortune(funnyJobsList,locationNames,partnerNames,numChildren))};
43+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
44+
console.log(" ");
45+
};
46+
47+
runExercise03();
48+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
/*
4+
description Exercise with array element adding & removing
5+
*/
6+
7+
function runExercise04 () {
8+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
9+
console.log("JS-1 - week 3 - Exercise 04 - Supermarket Shopping Cart");
10+
console.log("Array element adding & removing");
11+
console.log("- - - - - - - - - - - - - - - - - - - -");
12+
function addToShoppingCart(itemList,itemAdded){
13+
let funcResult=`In the supermarket your cart contains [${itemList}]. `;
14+
if (itemList.indexOf(itemAdded)<0) {
15+
itemList.push(itemAdded);
16+
funcResult+=`You also added [${itemAdded}] but noticed you have 3 items `
17+
+`so you removed [${itemList.shift()}].`;
18+
} else {
19+
funcResult+=`You tried to add [${itemAdded}] but noticed you already had some.`;
20+
}
21+
return funcResult+` You ended up buying [${itemList}].`;
22+
}
23+
let myShoppingList=['milk','bananas'];
24+
console.log(addToShoppingCart(myShoppingList,'coffee'));
25+
console.log(addToShoppingCart(myShoppingList,'bread'));
26+
console.log(addToShoppingCart(myShoppingList,'coffee'));
27+
console.log(addToShoppingCart(myShoppingList,'honey'));
28+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
29+
console.log(" ");
30+
};
31+
32+
runExercise04();
33+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict'
2+
3+
/*
4+
description exercise with aggregate (total sum calculation) of object property values
5+
*/
6+
7+
function runExercise05 () {
8+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
9+
console.log("JS-1 - week 3 - Exercise 05 - Total cost of Party shopping - Print the receipt");
10+
console.log("Aggregate (total sum calculation) of object property values");
11+
console.log("- - - - - - - - - - - - - - - - - - - -");
12+
function printReceipt(itemList){
13+
function printReceiptLine(lineText){console.log(' '.repeat(20)+'|'+lineText+'|')};
14+
const receiptWidth=30;
15+
function formatLine(leftText,rightText,doCenter){
16+
let funcResult=` ${leftText+rightText} `;
17+
while (funcResult.length<receiptWidth){
18+
funcResult=(doCenter?' '+funcResult+' ':funcResult.substr(0,leftText.length+2)
19+
+' '+funcResult.substr(leftText.length+2));
20+
}
21+
return funcResult.substr(0,receiptWidth);
22+
}
23+
printReceiptLine('='.repeat(receiptWidth));
24+
printReceiptLine(formatLine('Cash Register Receipt','',true));
25+
printReceiptLine('='.repeat(receiptWidth));
26+
printReceiptLine(formatLine('Product','Price',false));
27+
printReceiptLine('-'.repeat(receiptWidth));
28+
let valueList=Object.values(itemList);
29+
let descrList=Object.keys(itemList);
30+
let totalSum=0;
31+
for (let i=0; i<valueList.length; i++) {
32+
printReceiptLine(formatLine(descrList[i],valueList[i],false));
33+
totalSum+=valueList[i];
34+
}
35+
printReceiptLine('-'.repeat(receiptWidth));
36+
printReceiptLine(formatLine('Total cost',(Math.round(totalSum*100)/100),false));
37+
printReceiptLine('='.repeat(receiptWidth));
38+
}
39+
let cartForParty={ beer:12.99, chips:5.45, tequila:32.12, crackers:3.63, soda:5.89 };
40+
console.log('For your party you filled a shopping cart with the following items:');
41+
console.log(cartForParty);
42+
console.log('And here is the bill you have to pay:');
43+
printReceipt(cartForParty);
44+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
45+
console.log(" ");
46+
};
47+
48+
runExercise05();
49+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
3+
/*
4+
description PROJECT: Validate the number of a credit card based on specified ruleset
5+
*/
6+
7+
function runProjectccNumberValidator() {
8+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
9+
console.log("JS-1 - week 3 - PROJECT: Credit card number Validation");
10+
console.log("Validate the number of a credit card based on specified ruleset");
11+
console.log("- - - - - - - - - - - - - - - - - - - -");
12+
//
13+
// define the Credit card number validation routine
14+
//
15+
function isValidccNumber(ccNumber){
16+
// transform to string and remove non-numeric characters
17+
let ccNumStr=String(ccNumber).replace(/[^0-9]/g,'');
18+
// put each character into an array element
19+
let ccDigits=ccNumStr.split('');
20+
// decide validity based on specified ruleset
21+
return (ccNumStr.length==16)
22+
&& (ccNumStr.replace((new RegExp(ccNumStr[0],'g')),'').length>0)
23+
&& ((ccNumStr[15]%2)==0)
24+
&& (ccDigits.reduce((a,b)=>Number(a)+Number(b))>16);
25+
}
26+
let ccNumberList=[ '9999777788880000','6666666666661666',' 1234 - 5678 - 9012 - 3456 ',
27+
'a92332119c011112','4444444444444444',
28+
'1111111111111110','6666666666666661',' 6543 - 2109 - 8765 - 4321 ' ];
29+
for (let i=0; i<ccNumberList.length; i++){
30+
console.log(`Based on the provided rulset, the Credit card number [${
31+
ccNumberList[i]}] ${(isValidccNumber(ccNumberList[i])?'IS':'is NOT')} valid.`);
32+
}
33+
console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
34+
console.log(" ");
35+
};
36+
37+
runProjectccNumberValidator();
38+

Week3/homework/main.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
body {
3+
background-color: silver;
4+
padding: 0px 15px;
5+
font-size: 150%;
6+
font-family: sans-serif;
7+
}
8+
9+
.cls_text_align_left {
10+
text-align: left;
11+
}
12+
13+
.cls_text_align_right {
14+
text-align: right;
15+
}
16+
17+
.cls_text_align_center {
18+
text-align: center;
19+
}
20+
21+
.cls_background_color_gold {
22+
background-color: gold;
23+
}
24+
25+
div {
26+
padding:10px 20px;
27+
margin: 10px 20px
28+
}
29+
30+
;
31+

Week3/homework/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
const const_page_title="Homework: JS-1 - week 3";
3+
4+
document.getElementById("id_page_title").innerHTML=
5+
document.getElementById("id_page_caption").innerHTML=
6+
const_page_title;
7+

0 commit comments

Comments
 (0)