Untitled

 avatar
unknown
javascript
a year ago
3.8 kB
3
Indexable
// ! Task 1: Variables and Data Types
// Define three variables:
// A variable storing a String
// A variable storing a Number
// A variable storing a Boolean
let word = 'String';
let number = 10;
let logic = true;

// Log the data type of each variable to the console.
console.log(typeof(word), typeof(number), typeof(logic));

// Convert the Number variable to a String.
let numberString = String(number);
console.log(typeof(numberString));

// Update the value of the Boolean variable.
logic = false;
console.log(logic);

// Convert the string to uppercase.
console.log(word.toUpperCase());




// ! Task 2: Conditional Logic
// Provide examples of the following:
// An If Statement
if (logic) {
    console.log("True logic");
}


// An If / Else Statement
if (logic) {
    console.log("True logic");
} else {
    console.log("False logic");
}


// An If / Else If / Else Statement
if (number > 10) {
    console.log("Number is less than 10");
} else if (number > 5 < 10) {
    console.log("Number is greater than 5 but less than 10");
} else {
    console.log("Number must be greater than 10");
}


// A Switch Statement
switch (number) {
    case 1:
        console.log("number 1");
    case 2:
        console.log("number 2");
    case 3:
        console.log("number 3");
    case 4:
        console.log("number 4");
    case 5:
        console.log("number 5");
    case 6:
        console.log("number 6");
    case 7:
        console.log("number 7");
    case 8:
        console.log("number 8");
    case 9:
        console.log("number 9");
    case 10:
        console.log("number 10");
}


// Write a conditional statement that makes use of either && or ||
if (number == 10 && typeof(number) == 'number') {
    console.log("Requirments met");
}



// ! Task 3: Arrays
// Define an array to store the days of the week (Mon-Fri)
const weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
// Add “Saturday” and “Sunday” to the end of the array.
weekdays.push('Sat', 'Sun');
// Remove “Wednesday” from the array.
weekdays.splice(2,1);



// ! Task 4: Loops


// Using a loop, iterate over your “Weekday” array logging each day to the console.
for (day of weekdays) {
    console.log(day);
}


// Loop through your array and output to the console whether or not the day is during the week or the weekend.
for (day of weekdays) {
    switch (day) {
        case 'Mon':
            console.log(`${day} is a weekday`);
        case 'Tue':
            console.log(`${day} is a weekday`);
        case 'Wed':
            console.log(`${day} is a weekday`);
        case 'Thu':
            console.log(`${day} is a weekday`);
        case 'Fri':
            console.log(`${day} is a weekday`);
        case 'Sat':
            console.log(`${day} is a weekend`);
        case 'Sun':
            console.log(`${day} is a weekend`);
    }
}

// Write a loop that counts from 1-100 and checks whether each number is odd or even.

for (let i = 1; i <= 100; i++) {
    console.log(i);
}


// ! Task 5: Functions
// Write a function that takes in an array as an argument and sorts the array in ascending order.
function sortArray(array) {
    return array.sort();
}


// Write a function that takes in an array as an argument and removes the largest number from the array.
function removeLargestNum(array) {
    let sorted = array.sort(function (a, b) {
        return a - b;
    })

    return sorted.slice(-1)
}


// ! Task 6: Objects
// Create a pet object that contains details such as the name and type of animal.
const pet = {
    name: 'Peanut',
    type: 'Parrot',
    age: 3
}

// Add an “eat” method to the object that returns a string referencing the pet's name, saying “<petName> is eating.”

pet.eat = function () {
    return `${this.name} is eating`;
}

console.log(pet.eat());
Leave a Comment