Assignments On Day 2 - JavaScript Syntax and Basic Output

Rashmi Mishra
0

 

Assignments On  Day 2 

JavaScript Syntax and Basic Output

Assignment 1: Declaring and Logging Variables

Problem:

Declare three variables named firstName, lastName, and age using let, const, and var, respectively. Assign values to these variables as follows:

  • firstName should be your first name.
  • lastName should be your last name.
  • age should be your age.

Output each variable's value in the console.

Solution: 

let firstName = "John";

const lastName = "Doe";

var age = 25;

console.log("First Name:", firstName);

console.log("Last Name:", lastName);

console.log("Age:", age);

 Output:

 

Assignment 2: Using alert() for User Greeting

Problem:

Write a JavaScript program that declares a variable userName with your name as the value. Display an alert box that says "Hello, [userName]! Welcome to JavaScript."

Solution:

let userName = "Alice";

alert("Hello, " + userName + "! Welcome to JavaScript.");

Assignment3: Document Output with document.write()

Problem:

Create a script that declares two variables, courseName and duration. Set courseName to "JavaScript Basics" and duration to "2 weeks". Use document.write() to display the message on the webpage:

"This course, JavaScript Basics, will last for 2 weeks."

Solution:

let courseName = "JavaScript Basics";

let duration = "2 weeks";

document.write("This course, " + courseName + ", will last for " + duration + ".");

Assignment 4: Adding Comments to Explain Code

Problem:

Write a JavaScript program that calculates the area of a circle with a radius of 10. Use const to declare the value of pi (3.14159) and let to declare the radius. Write comments explaining each step of the code.

Solution:

// Declare a constant for pi

const pi = 3.14159;

// Declare a variable for the radius

let radius = 10;

// Calculate the area of the circle

let area = pi * radius * radius;

// Output the area to the console

console.log("The area of the circle is:", area); 

Assignment 5: Using Different Output Methods

Problem:

Declare a variable greeting with the value "Hello, World!". Output this variable using all three methods: console.log(), alert(), and document.write().

Solution:

let greeting = "Hello, World!"; 

// Output to the console

console.log(greeting);

// Display in an alert box

alert(greeting);

// Write to the webpage

document.write(greeting);

 Assignment 6: Practice with let, const, and var

Problem:

Create a JavaScript program where:

  • You declare a variable city with let and assign it the value "Paris".
  • You declare a constant country with const and assign it the value "France".
  • You declare a variable population with var and assign it the value 2,148,000.

Display these values in the console with a message like:

"Paris is in France with a population of 2148000."

Solution:

let city = "Paris";

const country = "France";

var population = 2148000; 

console.log(city + " is in " + country + " with a population of " + population + "."); 

Assignment 7: Calculation with Variables and Console Logging

Problem:

Write a JavaScript program that:

  • Declares a variable num1 with the value 15.
  • Declares a variable num2 with the value 30.
  • Calculates the sum, difference, product, and quotient of these two numbers.
  • Logs each result in the console with a descriptive message.

Solution:

let num1 = 15;

let num2 = 30;

let sum = num1 + num2;

let difference = num2 - num1;

let product = num1 * num2;

let quotient = num2 / num1;

console.log("Sum:", sum);

console.log("Difference:", difference);

console.log("Product:", product);

console.log("Quotient:", quotient); 

Assignment 8: Multi-line Comments for Documentation

Problem:

Write a script that:

  • Declares a constant greetingMessage with the value "Welcome to JavaScript class!".
  • Declares a variable currentDate with today’s date.
  • Outputs both values using console.log().

Write a multi-line comment at the top of the code explaining the purpose of the program.

Solution:

/*

  This script displays a greeting message and

  the current date in the console.

  The greeting message is stored in a constant,

  and the current date is stored in a variable.

*/

const greetingMessage = "Welcome to JavaScript class!";

let currentDate = new Date();

console.log("Message:", greetingMessage);

console.log("Today's Date:", currentDate);


Post a Comment

0Comments

Post a Comment (0)