Practice Assignments
JavaScript Practice Assignments: Values and Variables
Assignment 1: Simple Variables and Console Output
Task:
- Declare a variable
nameand assign it your name. - Declare a variable
ageand assign it your age. - Declare a constant
favoriteColorand 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,
num1andnum2, and assign them numerical values. - Create variables for the sum, difference, product, and quotient of
num1andnum2. - 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
firstNameand assign it your first name. - Declare a variable
lastNameand assign it your last name. - Concatenate
firstNameandlastNameto create a new variablefullName. - Print
fullNameto 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
isAdultand assign it a boolean value based on your age (true if 18 or older, false if younger). - Use an
if-elsestatement to print “You are an adult.” ifisAdultis true, and “You are not an adult.” ifisAdultis 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
numberscontaining five numerical values. - Use a
forloop 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
personwith propertiesname(your name),age(your age), andfavoriteColor(your favorite color). - Print each property of the
personobject 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
greetthat takes one parameternameand prints “Hello, [name]!” to the console. - Call the
greetfunction 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
countand initialize it with 0. - Create a function
incrementthat increases the value ofcountby 1 and prints the new value to the console. - Call the
incrementfunction 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
scoreand assign it a numerical value. - Write a function
gradethat prints “Pass” if the score is 50 or higher, and “Fail” otherwise. - Call the
gradefunction with thescorevariable 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
studentsas an array of objects, where each object represents a student with propertiesnameandage. - Use a
forloop 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!