diff --git a/package-lock.json b/package-lock.json index fe94e8b..5f10216 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "javascript-for-the-webflow-enthusiast-00", + "name": "javascript-bae-01", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "javascript-for-the-webflow-enthusiast-00", + "name": "javascript-bae-01", "version": "1.0.0", "license": "ISC", "devDependencies": { diff --git a/src/01-0-basics.js b/src/01-0-basics.js index c49ee8d..89c5438 100644 --- a/src/01-0-basics.js +++ b/src/01-0-basics.js @@ -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); } /** @@ -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)); } /** @@ -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); } /** @@ -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); } diff --git a/src/01-1-basics.js b/src/01-1-basics.js index 720c9d3..efd2626 100644 --- a/src/01-1-basics.js +++ b/src/01-1-basics.js @@ -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); } /** @@ -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() { @@ -34,6 +56,10 @@ function triangle() { ###### ####### */ + const char = "#"; + for (let i = 1; i <= 7; i++) { + console.log(char.repeat(i)); + } } /** @@ -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); }; /** @@ -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' } diff --git a/src/01-2-basics.js b/src/01-2-basics.js index 0c9f6d0..96df4d9 100644 --- a/src/01-2-basics.js +++ b/src/01-2-basics.js @@ -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(); +} \ No newline at end of file