Basic Variable Declaration and Logging
Declare and Log a String Variable
Create a variable named greeting and assign it the value "Hello, World!".
Log greeting to the console.
let greeting = "Hello, World!";
console.log(greeting);
Declare and Log a Number Variable
Create a variable named year and assign it your birth year.
Log year to the console.
let year = 2001;
console.log(year);
Declare and Log a Boolean Variable
Create a variable named isStudent and assign it true or false.
Log isStudent to the console.
let isStudent = true;
console.log(isStudent);
Through testing and playing around with the code in LeetCode Playground I have found out something! That in order for the variables value to be boolean it needs to be started with a small case;
Declare Multiple Variables
Create three variables: firstName, lastName, and fullName.
Assign your first and last names to firstName and lastName.
Log firstName and lastName to the console.
let firstName;
let lastName;
let fullName;
firstName = "Mateen";
lastName = "Khadem";
fullName = firstName + " " + lastName;
console.log(fullName);
Update a Variable’s Value
Create a variable named temperature and assign it 25.
Change the value of temperature to 30.
Log the updated temperature to the console.
let temperature = 25;
temperature = 30;
console.log(temperature);
Working with Arrays
Create and Log an Array of Colors
Create an array named colors containing three color names.
Log the colors array to the console.
let colors = ["Red", "Green", "Blue"];
console.log(colors);
Accessing Array Elements
Using the colors array from the previous exercise, log the first color.
Log the last color in the array.
let colors = ["Red", "Green", "Blue"];
console.log(colors[0]);
Array Length
Create an array named numbers with five numbers.
Log the length of the numbers array to the console.
let numbers = [1, 2, 3, 4, 5];
console.log(numbers.length);
Not that here length is a property not a function!
Empty Array and Logging
Create an empty array named emptyArray.
Log emptyArray to the console.
let emptyArray = [];
console.log(emptyArray);
Array with Mixed Data Types
Create an array named mixed containing a string, number, and boolean.
Log the mixed array to the console.
let mixed = ["Coding", 192, true];
console.log(mixed);
Combining Variables and Arrays
Assigning Variable to Array Element
Create an array named pets with two pet names.
Assign the first pet name to a variable called firstPet.
Log firstPet to the console.
let pets = ["Cat", "Dog"];
let firstPet = pets[0];
console.log(firstPet);
Updating Array Elements
Create an array named cities with three city names.
Change the second city to a different name.
Log the updated cities array.
let cities = ["Tehran", "Qom", "New York"];
cities[1] = "Isfahan";
console.log(cities);
Using Variables in Arrays
Create variables item1, item2, and item3 with any values.
Create an array named items that includes item1, item2, and item3.
Log the items array.
let item1 = "Food";
let item2 = "Sleep";
let item3 = "Medicine";
let items = [item1, item2, item3];
console.log(items);
Array of Variables
Create three variables: drink1, drink2, drink3 with your favorite beverages.
Create an array named drinks containing these three variables.
Log drinks to the console.
let drink1 = "Chai";
let drink2 = "Coca";
let drink3 = "Water";
let drinks = [drink1, drink2, drink3];
console.log(drinks);
Logging Specific Array Elements Using Variables
Create an array named books with four book titles.
Create a variable thirdBook that holds the third element of the books array.
Log thirdBook to the console.
let books = ["Bible", "Notes from Underground", "The Idiot", "Harry Potter"];
console.log(books[2]);
Basic Array Operations
Adding Elements to an Array
Create an array named languages with two programming languages.
(Note: Since you’re only using let, you might simulate adding by reassigning.)
Log the updated languages array.
let languages = ["JavaScript", "Python"];
languages[2] = "PHP";
console.log(languages);
Removing Elements from an Array
Create an array named animals with three animal names.
Remove the last animal by setting its index to undefined or another method.
Log the updated animals array.
let animals = ["Cat", "Dog", "Ant"];
animals[2] = undefined;
console.log(animals);
This method doesn’t actually remove the last animal but the following method does.
let animals = ["Cat", "Dog", "Ant"];
animals.pop();
console.log(animals);
Replacing Array Elements
Create an array named vehicles with three types of vehicles.
Replace the second vehicle with a different one.
Log the vehicles array.
let vehicles = ["Car", "Bike", "Train"];
vehicles[1] = "Airplane";
console.log(vehicles);
Accessing Array Elements by Index
Create an array named letters with letters "A", "B", "C", "D".
Log the letter at index 2 to the console.
let letters = ["A", "B", "C", "D"];
console.log(letters[2]);
Logging All Array Elements Individually
Create an array named numbers with four numbers.
Log each number separately using its index.
let numbers = [1, 2, 3, 4];
console.log(numbers[0]);
console.log(nubmers[1]);
console.log(nubmers[2]);
console.log(nubmers[3]);
Combining Multiple Variables and Arrays
Creating a Profile Object with Variables (Simulated with Arrays)
Create variables name, age, and city.
Create an array named profile that includes these variables.
Log the profile array.
let name = "Mateen";
let age = 23;
let city = "Qom";
let profile = [name, age, city];
Swapping Variable Values Using an Array
Create two variables a and b with different values.
Use an array to swap their values.
Log both a and b after swapping.
let a = 23;
let b = "Abc";
let c = [23, "Abc"];
a = c[1];
b = c[0];
console.log(a);
console.log(b);
Storing Student Names in an Array
Create three variables student1, student2, student3 with student names.
Create an array students containing these variables.
Log the students array.
let student1 = "Ali";
let student2 = "Muhammad";
let student3 = "Jason";
let students = [student1, student2, student3];
console.log(students);
Calculating the Total Using Variables and an Array
Create three number variables: num1, num2, num3.
Create an array nums containing these variables.
Log nums to the console.
let num1 = 1;
let num2 = 2;
let num3 = 3;
let nums = [num1, num2, num3]
console.log(nums);
Favorite Movies Array
Create five variables movie1 to movie5 with your favorite movies.
Create an array favoriteMovies containing these variables.
Log favoriteMovies.
let movie1 = "The Notebook";
let movie2 = "The Social Network";
let movie3 = "Harry Potter";
let movie4 = "How I met Your Mother";
let movie5 = "Whatever";
let favoriteMovies = [movie1, movie2, movie3, movie4, movie5];
console.log(favoriteMovies);
Advanced Variable and Array Manipulations
Nested Arrays (Arrays within Arrays)
Create two arrays: breakfast with foods and lunch with foods.
Create a main array meals that contains breakfast and lunch arrays.
Log the meals array.
let breakfast = ["Eggs", "Salami", "Tea"];
let lunch = ["Pizza", "Burger"];
let meals = [breakfast, lunch];
console.log(meals);
Accessing Elements in Nested Arrays
Using the meals array from the previous exercise, log the first item of the lunch array.
let breakfast = ["Eggs", "Salami", "Tea"];
let lunch = ["Pizza", "Burger"];
let meals = [breakfast, lunch];
console.log(lunch[0]);
Creating an Array of Arrays with Variables
Create three arrays each containing different types of data (e.g., numbers, strings, booleans).
Create a main array collection that holds these three arrays.
Log collection to the console.
let a = "Abc";
let b = 2;
let c = false;
let collection = [a, b, c];
console.log(collection);
Assigning Array Elements to Separate Variables
Create an array coordinates with three numbers representing x, y, z.
Assign each element to separate variables x, y, and z.
Log x, y, and z individually.
let coordinates = [2, 23,234];
let x = coordinates[0];
let y = coordinates[1];
let z = coordinates[2];
console.log(x);
console.log(y);
console.log(z);
Combining Strings from Variables and Arrays
Create a variable greeting with the value "Hello".
Create an array names with three different names.
Log a greeting message for each name, e.g., "Hello, Alice!" using console.log.
let greeting = "Hello";
let names = ["Ali", "Muhammad", "Hussain"];
console.log(greeting + ", " + names[0] + "!")
console.log(greeting + ", " + names[1] + "!")
console.log(greeting + ", " + names[2] + "!")