Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed all tasks for unit 01. #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/01-0-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
*/

// 1. Define three string variables firstName, middleName, and lastName.
var firstName = "Sung-Duk";
var middleName = "Duck";
var lastName = "Kang";

// 2. Declare a function named logFullName that takes no arguments.
function logFullName() {
// 3. Using template literals, create another variable fullName that combines all three names.
// 4. Print the fullName to the console.
var fullName = `${firstName} ${middleName} ${lastName}`;
console.log(fullName);
}

/**
Expand All @@ -17,10 +22,16 @@ function logFullName() {
// 1. Declare a variable named age and assign it a number.
// 2. Declare a variable named isStudent and assign it a boolean value.
// 3. Declare a variable named courses and assign it an array containing three string values representing courses e.g. "Math", "Science", "History".
var age = 29;
var isStudent = true;
var courses = ["Math", "Science", "History"];

// 4. Declare a function named logVariableTypes that takes no arguments.
function logVariableTypes() {
// 5. Print the type of each variable using the typeof operator.
console.log(typeof(age));
console.log(typeof(isStudent));
console.log(typeof(courses));
}

/**
Expand All @@ -30,26 +41,35 @@ function logVariableTypes() {
// 1. Using var, declare a variable named school and assign it a value of "Hogwarts".
// 2. Using let, declare a variable named subject and assign it a value of "Potions".
// 3. Using const, declare a variable named professor and assign it a value of "Snape".
var school = "Hogwarts";
let subject = "Potions";
const professor = "Snape";

/**
* Task 4: Basic Operators
*/

// 1. Declare two variables x and y with values 5 and 10 respectively.
let x = 5;
let y = 10;

function logAddition() {
// 2. Log the sum of x and y.
console.log(x + y);
}
function logSubtraction() {
// 3. Log the x subtracted from y
console.log(y - x);
}

function logMultiplication() {
// 4. Log the product of x and y.
console.log(x * y);
}

function logDivision() {
// 5. Log the quotient when x is divided by y.
console.log(x / y);
}

/**
Expand All @@ -64,4 +84,6 @@ let result2 = (3 + 4) * 5;

function logResults() {
// 3. Log both result values in your JavaScript environment and check your answers.
console.log(result1);
console.log(result2);
}
42 changes: 42 additions & 0 deletions src/01-1-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@
*/

// Define a variable named studentGrade that is of type number.
let studentGrade = 95;

function gradeCommentary() {
// 1. If studentGrade is above or equal to 90, log "Excellent work!".
// 2. If studentGrade is between 80 and 89 (inclusive), log "Good job!".
// 3. If studentGrade is between 70 and 79 (inclusive), log "You passed.".
// 4. If studentGrade is below 70, log "Needs improvement.".
let comment = "";
if (studentGrade >= 90) {
comment = "Excellent work!";
} else if (studentGrade <= 89 && studentGrade >= 80) {
comment = "Good job!";
} else if (studentGrade <= 79 && studentGrade >= 70) {
comment = "You passed.";
} else {
comment = "Needs improvement."
}
console.log(comment);
}

/**
Expand All @@ -17,10 +29,20 @@ function gradeCommentary() {

function logNumbers() {
// 1. Log numbers from 1 to 10 using a for loop.
for (let i = 1; i <= 10; i++) {
console.log(i);
}
}

function countByTwos() {
// 2. Using a while loop, log even numbers from 2 to 20.
let i = 2;
while (i <= 20) {
if (i % 2 == 0) {
console.log(i);
}
i += 2;
}
}

function triangle() {
Expand All @@ -34,6 +56,10 @@ function triangle() {
######
#######
*/
const char = "#";
for (let i = 1; i <= 7; i++) {
console.log(char.repeat(i));
}
}

/**
Expand All @@ -43,11 +69,19 @@ function triangle() {
// 1. Declare a function named calculateArea that takes two arguments: length and width. It should return the area of a rectangle.
function calculateArea(length, width) {
// Your code here
const area = length * width;
if (area < 0) {
return NaN;
} else {
return area;
}
}

// 2. Declare a function expression named greetPerson that takes a name as an argument and logs a greeting.
var greetPerson = function (name) {
// Your code here
const greeting = `Hello, ${name}!`;
console.log(greeting);
};

/**
Expand All @@ -57,19 +91,27 @@ var greetPerson = function (name) {
// 1. Declare an object named book with properties: title, author, and year.
var book = {
// Your properties here
title: "Lord of the Rings",
author: 'J.R.R. Tolkin',
year: 1932
};

// 2. Log the book's title.
function logBookTitle() {
// Your code here
console.log(book.title)
}

// 3. Update the book's year to the current year.
function updateYear() {
// Your code here
const today = new Date();
const currYear = today.getFullYear()
book.year = currYear;
}

// 4. Add a new property to the book: genre, and assign it a value.
function addGenre() {
// Your code here
book.genre = 'Fiction'
}
12 changes: 12 additions & 0 deletions src/01-2-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ function generateBandName(clothingColor, lastFoodEaten) {
// Initialize bandName to an empty string
// Function to capitalize the first letter of each word
// Construct the band name
let bandName = "";
const color = clothingColor ? capitalize(clothingColor + "") : "";
const food = lastFoodEaten ? capitalize(lastFoodEaten + "") : "";
bandName = `The ${color} ${food}`;
return bandName;
}

// Takes in a word as a string and returns it with the first letter capitalized and
// the rest lower case.
function capitalize(word) {
if (!word.length) return;
return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase();
}
Loading