Practice Assignments
JavaScript Practice Assignments: Values and Variables
Assignment 1: Simple Variables and Console Output
Task:
- Declare a variable
name
and assign it your name. - Declare a variable
age
and assign it your age. - Declare a constant
favoriteColor
and assign it your favorite color. - Print each of these values to the console using
console.log()
.
Example Solution:
let name = 'John Doe';
let age = 25;
const favoriteColor = 'blue';
console.log('Name:', name);
console.log('Age:', age);
console.log('Favorite Color:', favoriteColor);
Assignment 2: Arithmetic Operations
Task:
- Declare two variables,
num1
andnum2
, and assign them numerical values. - Create variables for the sum, difference, product, and quotient of
num1
andnum2
. - Print the results of these operations to the console.
Example Solution:
let num1 = 10;
let num2 = 5;
let sum = num1 + num2;
let difference = num1 - num2;
let product = num1 * num2;
let quotient = num1 / num2;
console.log('Sum:', sum);
console.log('Difference:', difference);
console.log('Product:', product);
console.log('Quotient:', quotient);
Assignment 3: Working with Strings
Task:
- Declare a variable
firstName
and assign it your first name. - Declare a variable
lastName
and assign it your last name. - Concatenate
firstName
andlastName
to create a new variablefullName
. - Print
fullName
to the console.
Example Solution:
let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName;
console.log('Full Name:', fullName);
Assignment 4: Boolean Values and Conditions
Task:
- Declare a variable
isAdult
and assign it a boolean value based on your age (true if 18 or older, false if younger). - Use an
if-else
statement to print “You are an adult.” ifisAdult
is true, and “You are not an adult.” ifisAdult
is false.
Example Solution:
let age = 25;
let isAdult = age >= 18;
if (isAdult) {
console.log('You are an adult.');
} else {
console.log('You are not an adult.');
}
Assignment 5: Arrays and Loops
Task:
- Declare an array
numbers
containing five numerical values. - Use a
for
loop to iterate over the array and print each number to the console.
Example Solution:
let numbers = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
console.log('Number:', numbers[i]);
}
Assignment 6: Objects and Properties
Task:
- Create an object
person
with propertiesname
(your name),age
(your age), andfavoriteColor
(your favorite color). - Print each property of the
person
object to the console.
Example Solution:
let person = {
name: 'John Doe',
age: 25,
favoriteColor: 'blue'
};
console.log('Name:', person.name);
console.log('Age:', person.age);
console.log('Favorite Color:', person.favoriteColor);
Assignment 7: Functions
Task:
- Write a function
greet
that takes one parametername
and prints “Hello, [name]!” to the console. - Call the
greet
function with your name as the argument.
Example Solution:
function greet(name) {
console.log('Hello, ' + name + '!');
}
greet('John Doe');
Assignment 8: Updating Variables
Task:
- Declare a variable
count
and initialize it with 0. - Create a function
increment
that increases the value ofcount
by 1 and prints the new value to the console. - Call the
increment
function three times.
Example Solution:
let count = 0;
function increment() {
count++;
console.log('Count:', count);
}
increment(); // Count: 1
increment(); // Count: 2
increment(); // Count: 3
Assignment 9: Conditional Logic
Task:
- Declare a variable
score
and assign it a numerical value. - Write a function
grade
that prints “Pass” if the score is 50 or higher, and “Fail” otherwise. - Call the
grade
function with thescore
variable as an argument.
Example Solution:
let score = 65;
function grade(score) {
if (score >= 50) {
console.log('Pass');
} else {
console.log('Fail');
}
}
grade(score);
Assignment 10: Combining Data Types
Task:
- Declare a variable
students
as an array of objects, where each object represents a student with propertiesname
andage
. - Use a
for
loop to iterate over the array and print each student’s name and age to the console.
Example Solution:
let students = [
{ name: 'Alice', age: 20 },
{ name: 'Bob', age: 22 },
{ name: 'Charlie', age: 23 }
];
for (let i = 0; i < students.length; i++) {
console.log('Name:', students[i].name);
console.log('Age:', students[i].age);
}
By completing these assignments, you’ll gain practical experience with values, variables, and basic operations in JavaScript. Feel free to modify and expand these examples to further enhance your understanding!