Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions JavaScripHYF/exercise.smart-ease.index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>Test text</p>
<script src="exercise.smart-ease.js"></script>
</body>
</html>

148 changes: 148 additions & 0 deletions JavaScripHYF/exercise.smart-ease.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//The full name exercise.
function getFullName(firstName, lastName,useFormalName ) {
let fullName =` ${firstName} ${lastName}`

return useFormalName ? `Lord ${fullName}` : fullName;

}

const fullName1 = getFullName("John", "Doe");
const fullName2 = getFullName("Jane", "Smith");

console.log(fullName1);
console.log(fullName2);

// Event application
function getEventWeekday(numberOfDaysFromToday) {
const today = new Date();
const targetDate = new Date(today);
targetDate.setDate(today.getDate() + numberOfDaysFromToday);

const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const weekday = weekdays[targetDate.getDay()];

return weekday;
}

console.log(getEventWeekday(9));

// Weather wear
function getWearByWeather(temperature) {
if (temperature < 0) {
return "Heavy winter coat, gloves, and a hat";
} else if (temperature >= 0 && temperature < 10) {
return "Winter jacket and warm layers";
} else if (temperature >= 10 && temperature < 18) {
return "Light jacket or sweater";
} else
return "Shorts and a t-shirt";
}

const clothesToWear = getWearByWeather(18);
console.log(clothesToWear);

//Student menager
const class07Students = [];

function addStudentToClass(studentName) {

if (studentName === 'Queen Mary') {
class07Students.push(studentName);
return;
}

if (class07Students.includes(studentName)) {
console.log(`Student ${studentName} is already in the class`);
return;
}

if (studentName=== '') {
console.log('You cannot add an empty name');
return;
}

if(class07Students.length >= 6) {
console.log('Cannot add more students to class 07');
return;
}


class07Students.push(studentName);

}

function getNumberOfStudents() {
return class07Students.length;
}

addStudentToClass('Alice');
addStudentToClass('Bob');
addStudentToClass('Charlie');
addStudentToClass('David');
addStudentToClass('Eve');
addStudentToClass('Frank');
addStudentToClass('Grace');
addStudentToClass('Queen Mary');
addStudentToClass('');
addStudentToClass('Alice');
addStudentToClass('Queen Mary');

console.log(getNumberOfStudents());

//Candy helper
const boughtCandyPrices = [];

function addCandy(candyType, weight){
let price;
if(candyType === "sweet"){
price = 0.5 * weight;
} else if(candyType === "chocolate"){
price = 0.7 * weight;
} else if(candyType === "toffee"){
price = 1.1 * weight;
} else if(candyType === "chewing-gum"){
price = 0.03 * weight;
}
boughtCandyPrices.push(price);

}

let amountToSpend = Math.random() * 100;

function canBuyMoreCandy(){
let totalSpent = 0;
for(let i = 0; i < boughtCandyPrices.length; i++){
totalSpent += boughtCandyPrices[i];
}
return totalSpent < amountToSpend;
}

addCandy("sweet", 10);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}


addCandy("chocolate", 5);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}

addCandy("toffee", 2);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}

addCandy("chewing-gum", 50);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}

177 changes: 177 additions & 0 deletions JavaScripHYF/exercise.voice-assistant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@

let userName = null;
let todos = [];

function getReply(command) {
console.log("Command received: ", command);

if (command.includes("hello my name is")) {
let words = command.split(" ");
let nameIndex = words.length - 1;
let name = words[nameIndex];

if (userName == name) {
return "Hi again " + userName;
}

userName = name;
return "Nice to meet you " + userName;
}

if (command.includes("what is my name")) {
if (userName) {
return userName;
} else {
return "I don't know your name";
}
}

if (command.includes("to my to do")) {
let todo = command.slice(4, -11)
todos.push(todo);
return `${todo} added to your todo`;
}

if (command.includes("from my todo")) {
let todo = command.slice(7, -1313);
let indexOfTodo = todos.indexOf(todo);
if (indexOfTodo !== -1) {
todos.splice(indexOfTodo, 1);
}

return `Removed ${todo} from your todo`;
}

if (command.includes("what is on my to do")) {
let numberOfItems = todos.length;
let responseSentence = `you have ${numberOfItems} todos - `;

for (let i = 0; i < todos.length; i++) {
if (todos.length > 1) {
responseSentence += `${todos[i]}, `;
} else {
responseSentence += `${todos[i]} `;
}
}
return responseSentence;
}

if (command.includes("what day is it today")) {
const date = new Date();
let day = date.getDate();

const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];

let month = monthNames[date.getMonth()];

let year = date.getFullYear();

return `${day} of ${month} ${year}`;
}

if (command.includes("what is ")) {
let fullOperationSentence = command.slice(8);
let partsOfSentence = fullOperationSentence.split(" ");

let operatorSymbol = partsOfSentence[1];
let number1 = parseInt(partsOfSentence[0]);
let number2 = parseInt(partsOfSentence[2]);

let result;

switch (operatorSymbol) {
case "+":
result = number1 + number2;
break;

case "-":
result = number1 - number2;
break;

case "*":
result = number1 * number2;
break;

case "/":
result = number1 / number2;
break;

default:
break;
}

return result;
}

if (command.includes("set a timer for")) {
let timerSentence = command.slice(16);
let partsOfSentence = timerSentence.split(" ");
let number = parseInt(partsOfSentence[0]);
let duration = partsOfSentence[1]; // seconds, minutes, hours

let timerInMilliSeconds;

switch (duration) {
case "seconds":
timerInMilliSeconds = number * 1000;
break;

case "minutes":
timerInMilliSeconds = number * 1000 * 60;
break;

case "hours":
timerInMilliSeconds = number * 1000 * 60 * 60;
break;

default:
break;
}


function timerFunction() {
artyom.say("Timer Done");
}

setTimeout(timerFunction, timerInMilliSeconds);

return `Timer set for ${number} ${duration}`;
}

return "I didn't understand that command";
}

// getReply("hello my name is Monika");
// getReply("hello my name is Monika");
// getReply("hello my name is Benjamin");

// getReply("what is my name");

// getReply("add fishing to my todo");
// getReply("add singing in the shower to my todo");
// getReply("add boating to my todo");
// getReply("add watching TV to my todo");

// getReply("remove fishing from my todo");

// getReply("what is on my todo");

// getReply("what day is it today");

// getReply("what is 3 + 3");
// getReply("what is 4 * 12");

// getReply("set a timer for 5 seconds")
10 changes: 10 additions & 0 deletions JavaScripHYF/exercise.week3.index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./exercise.week3.js"></script>
</body>
</html>
Loading