javascript-assignments.docx
javascript-assignments.docx
Console":
2. Type Conversion:
- Create a program that demonstrates implicit and explicit type conversion. Include examples
where you convert an integer to a float, a float to an integer, a string to an integer, and an
integer to a string. Print the results to the console.
1. Basic Output:
- Write a program that displays a welcome message and your favorite quote using console
output.
2. Formatted Output:
- Develop a program that accepts a user's first name, last name, and year of birth as inputs.
Display a formatted message that includes their full name and age (assuming the current year
is 2024).
4. Conditional Display:
- Write a program that takes an integer input from the user and displays whether it is even or
odd using a conditional statement.
5. Loop-Based Display:
- Develop a program that uses a loop to display the multiplication table for a number
provided by the user (up to 10 times the number). Ensure the output is neatly formatted.
6. Customized Greeting:
- Write a program that asks the user for their name and the current time (in hours). Based on
the time, display a customized greeting: "Good Morning", "Good Afternoon", or "Good
Evening".
1. Practical Question:
- Declare a variable and assign each of the following data types to it: number, string, boolean,
undefined, null, object, and array. Log the type of each variable using `console.log`.
### Commenting
2. Practical Question:
- Write a JavaScript function that calculates the factorial of a number. Use both single-line
and multi-line comments to explain each step of your code.
### Identifiers
3. Practical Question:
- Create variables with valid identifiers to store the following information: a user's first name,
last name, age, and email address. Ensure that each variable follows JavaScript naming
conventions.
### Keywords
4. Practical Question:
- Write a small script that uses at least five different JavaScript keywords appropriately.
Explain what each keyword does in a comment next to it.
5. Practical Question:
- Declare three variables using `let`, `const`, and `var`, and demonstrate the difference in
their scope within a function and a block.
6. Practical Question:
- Write a function that takes a single argument and returns the type of the argument. Test
this function with different data types and log the results.
7. Practical Question:
- Create a script that performs the following mathematical operations: addition, subtraction,
multiplication, division, and modulus. Log the results of each operation.
8. Practical Question:
- Write a script that demonstrates the use of different assignment operators (`=`, `+=`, `-=`,
`*=`, `/=`, `%=`). Explain what each operator does in a comment.
9. Practical Question:
- Create a complex expression using multiple operators. Log the result and explain the order
of operations that led to that result.
### Functions
5. Function Declaration:
- Write a function declaration for a function named `subtract` that takes two arguments and
returns their difference. Call this function with the arguments 10 and 4, and print the result.
6. Function Expression:
- Create a function expression for a function named `divide` that takes two parameters and
returns their quotient. Assign this function to a variable named `divideFunction`. Call
`divideFunction` with the arguments 20 and 4, and print the result.
1. Calculator Functions:
- Write a function `add(a, b)` that returns the sum of two numbers.
- Write a function `subtract(a, b)` that returns the difference between two numbers.
- Write a function `multiply(a, b)` that returns the product of two numbers.
- Write a function `divide(a, b)` that returns the division of two numbers.
- Write a main function `calculator()` that takes two numbers and an operator (as a string) as
inputs and calls the appropriate function from the ones defined above to perform the
calculation. Print the result.
2. Temperature Conversion:
- Write a function `celsius_to_fahrenheit(celsius)` that converts Celsius to Fahrenheit.
- Write a function `fahrenheit_to_celsius(fahrenheit)` that converts Fahrenheit to Celsius.
- Write a main function `convert_temperature(temp, scale)` that takes a temperature value
and a scale ('C' for Celsius, 'F' for Fahrenheit) and calls the appropriate conversion function.
Print the result.
1. Factorial Calculation:
- Write a recursive function `factorial(n)` that returns the factorial of a given number.
- Write a main function `print_factorials()` that prints the factorials of numbers from 1 to 10
using the `factorial` function.
2. Palindrome Check:
- Write a function `is_palindrome(s)` that checks if a given string `s` is a palindrome (reads
the same forward and backward).
- Write a main function `check_palindromes(words)` that takes a list of words and uses the
`is_palindrome` function to check each word. Print whether each word is a palindrome or not.
1. Array Reversal:
- Write a function `reverse_array(arr)` that takes an array and returns a new array with the
elements in reverse order.
- Write a main function that initializes an array and prints both the original and reversed
arrays using `reverse_array`.
3. Array Sorting:
- Write a function `sort_array(arr)` that sorts an array in ascending order without using
built-in sorting functions.
- Write a main function that initializes an array, prints the unsorted array, calls `sort_array`,
and then prints the sorted array.
2. Object Manipulation:
- Add a new property `address` to the `student` object with a nested object containing
`street`, `city`, and `zipCode`.
9. External Access:
- Write a function that takes an object and a new value, then updates the `name` property of
the object to the new value. Test this function on the `student` object.
1. Creating a Modal Dialog Box: Write a JavaScript function to create a modal dialog box that
displays a message when a button is clicked. The modal should include a "Close" button that
hides the modal when clicked.
2. Event Listener for Modal: Modify your modal dialog box to include an event listener that
triggers a console log message when the modal is opened and another message when it is
closed.
3. Form Submission in Modal: Create a modal dialog box containing a simple form (e.g., name
and email fields). Use event listeners to handle the form submission, validate the inputs, and
display a success message within the modal.
4. Variable Scope: Write a script that demonstrates the difference between local and global
variables. Define a global variable and a local variable with the same name inside a function.
Log their values inside and outside the function to show the difference.
5. Nested Functions and Scope: Create a function that contains another function inside it.
Demonstrate how variables are scoped within the inner and outer functions by logging their
values at different points.
6. Variable Hoisting: Write a script that demonstrates variable hoisting by declaring and
initializing variables before and after they are used. Log the results to show how JavaScript
handles variable hoisting.
7. Function Hoisting: Create a script with two functions: one function declaration and one
function expression. Show how hoisting affects the ability to call these functions before their
definitions in the code.
8. Understanding `this` in Functions: Write a function within an object and use the `this`
keyword to access a property of the object. Log the value of the property to demonstrate how
`this` works in the context of an object method.
9. Arrow Functions and `this`: Create a script that includes both a regular function and an
arrow function as methods of an object. Use `this` inside both functions and log the results to
show the difference in how `this` is bound in regular functions versus arrow functions.
10. `this` in Event Listeners: Write a script that attaches an event listener to a button. Within
the event listener function, use `this` to reference the button element that was clicked. Log the
`this` keyword to the console to demonstrate its context within the event listener.
6. Comparison:
- Write a comparison function to demonstrate the difference between comparing primitives
and objects. Use `===` and `==` for comparisons and explain the outcomes.
8. Type Conversion:
- Create a function that takes a single argument and logs its type. Test this function with
various primitive types and document any implicit type conversions that occur.
1. Function Comparison:
- Write a regular function `greetRegular` that takes a name as an argument and returns a
greeting string.
- Write an arrow function `greetArrow` that performs the same operation.
- Call both functions with the name "Alice" and log the results to the console.
2. Lexical `this`:
- Create an object `person` with a name property and two methods: `sayHelloRegular` (using
a regular function) and `sayHelloArrow` (using an arrow function). Both methods should log a
greeting message using the `name` property.
- Compare the output of both methods when called from the `person` object.
3. Context Binding:
- Write a function `Counter` using a regular function that increments a `count` property every
second. Use `setInterval` for this purpose.
- Convert the `Counter` function to use an arrow function instead and observe any
differences in behavior.
1. Summation Function:
- Write a function `sumArguments` using the `arguments` keyword that sums all the
arguments passed to it.
- Convert the function to use rest parameters (`...args`) instead of the `arguments` keyword.
2. Dynamic Arguments:
- Create a function `logArguments` that logs each argument passed to it using the
`arguments` keyword.
- Test the function by passing different numbers and types of arguments (e.g., numbers,
strings, objects).
1. Primitive Assignment:
- Create two variables `a` and `b`, where `a` is a primitive type (e.g., a number or string) and
`b` is assigned the value of `a`.
- Modify `a` and log both `a` and `b` to the console to observe the behavior.
2. Object Assignment:
- Create two variables `obj1` and `obj2`, where `obj1` is an object and `obj2` is assigned the
value of `obj1`.
- Modify a property of `obj1` and log both `obj1` and `obj2` to the console to observe the
behavior.
1. Type Identification:
- Write a function `identifyType` that takes a value and logs its type using `typeof`.
- Test the function with different primitive types (number, string, boolean, null, undefined,
symbol, bigint).
2. Default Values:
- Create a variable for each primitive type and log their default values to the console.
#### Topic 5: Reference Types
1. Array Manipulation:
- Create an array `arr` and add some elements to it.
- Create another variable `arrCopy` and assign `arr` to it.
- Modify `arr` and log both `arr` and `arrCopy` to observe the behavior.
2. Object Manipulation:
- Create an object `obj` with some properties.
- Create another variable `objCopy` and assign `obj` to it.
- Modify `obj` and log both `obj` and `objCopy` to observe the behavior.
1. Restaurant Object:
- Create an object `restaurant` with properties: `name`, `location`, `categories` (array), and
`menu` (array of objects with properties `dish` and `price`).
- Add methods to add a new dish to the menu, remove a dish, and print the entire menu.
2. Order Handling:
- Add a method `placeOrder` to the `restaurant` object that takes a list of dish names,
calculates the total price, and returns an order summary.
- Create some sample orders and test the method.
1. Interactive Form:
- Create an HTML file with a `textarea` and a `button`.
- Write JavaScript code to alert the content of the `textarea` when the button is clicked.
2. Character Counter:
- Enhance the HTML file by adding a character counter that updates as the user types in the
`textarea`.
- Write JavaScript code to update the counter dynamically.
3. Input Validation:
- Add validation to the `textarea` to ensure it is not empty when the button is clicked.
- Display an error message if the validation fails.
2. Task: Create a dynamic list where users can add items using an input field and a button. Each
item should have a 'remove' button to delete that specific item.
- Requirements:
- Use `querySelector` to select the input field and button.
- Use `parentNode.removeChild` to remove the list item when the 'remove' button is
clicked.
#### 3. onselect
7. Task: Create a text area. When the user selects text within the text area, display the selected
text in an alert box.
- Requirements:
- Use `onselect` event handler.
- Use `window.getSelection` or `textarea.value.substring(textarea.selectionStart,
textarea.selectionEnd)` to get the selected text.
#### 4. onchange
8. Task: Create a dropdown (select) element. When the user selects an option, display the
selected value in a paragraph below the dropdown.
- Requirements:
- Use `onchange` event handler.
- Use `querySelector` to select the dropdown and paragraph.
- Use `textContent` or `innerHTML` to display the selected value.
### 1. Form Validation and Its Related Functions (UserDefined & Builtin)
- Assignment: Create a registration form with fields for username, email, and password.
Implement form validation to:
- Ensure the username is at least 5 characters long.
- Validate the email using a regular expression.
- Ensure the password is at least 8 characters long and contains at least one number and one
special character.
- Use both user-defined and built-in validation methods.
### Implementing Prototype Property in Creating New Properties outside the Object Literals
6. Prototype Property:
- Create a constructor function for `Person` objects with properties `name` and `age`. Add a
method to the prototype to display the person's details. Then, create a new method outside
the constructor to update the person's age.
8. Prototype Chain:
- Create a chain of prototype inheritance. Create a base object with a method, then extend
this base object to a derived object, and demonstrate accessing methods up the prototype
chain.
### ES Concepts: Creating Class, Class Declaration with Properties and Methods
13. Class Declaration:
- Create a class `Rectangle` with properties `width` and `height`. Add a method to calculate
the area of the rectangle. Create an instance and demonstrate the use of the class.
Objective:
Create a responsive registration form with advanced features and validation using HTML5 and
CSS3.
Requirements:
1. Form Structure:
- First Name, Last Name (text inputs)
- Username (text input)
- Email (email input)
- Password (password input)
- Confirm Password (password input)
- Date of Birth (date input)
- Gender (radio buttons: Male, Female, Other)
- Country (dropdown)
- Interests (checkboxes: multiple options)
- Profile Picture (file upload)
- Short Bio (textarea)
- Terms and Conditions (checkbox)
- Submit and Reset buttons
2. HTML5 Features:
- Use the `required` attribute to make fields mandatory.
- Use the `pattern` attribute for custom validation on Username (e.g., only alphanumeric
characters).
- Use `maxlength` and `minlength` for Username and Password.
- Use `type="email"` for the email input.
- Use `type="date"` for the date of birth.
- Use `input` elements with the `type="radio"` for gender selection.
- Use `input` elements with the `type="checkbox"` for interests.
3. CSS3 Features:
- Use flexbox or grid for form layout.
- Style the form with a visually appealing design, including hover effects on buttons.
- Use media queries to ensure the form is responsive.
- Implement custom styles for input focus states.
- Style validation messages using `:invalid` and `:valid` pseudo-classes.
- Add custom styles for the file upload input.
Objective:
Create a multi-step form that includes a progress bar to indicate the user's progress through
the form.
Requirements:
1. Form Steps:
- Step 1: Personal Information (Name, Email, Phone)
- Step 2: Address Information (Address Line 1, Address Line 2, City, State, ZIP Code)
- Step 3: Account Information (Username, Password, Confirm Password)
- Step 4: Confirmation
2. HTML5 Features:
- Use `fieldset` and `legend` to group related elements.
- Use `type="email"` for email input.
- Use `type="tel"` for phone input with a pattern for validation.
- Use the `required` attribute for all mandatory fields.
- Use `input` elements with the `type="text"` for address fields.
3. CSS3 Features:
- Implement a horizontal progress bar that updates as the user moves through the steps.
- Use transitions to animate the progress bar.
- Style the form steps using flexbox or grid for alignment.
- Create a visually appealing design with hover and focus effects.
- Implement media queries to ensure the form is responsive.
4. JavaScript (Optional):
- Add JavaScript to handle form navigation (Next/Previous buttons).
- Update the progress bar dynamically as the user moves through the steps.
Objective:
Create a survey form that shows or hides sections based on the user's previous answers.
Requirements:
1. Form Structure:
- User Information (Name, Age, Email)
- Survey Question 1: Do you own a car? (Yes/No radio buttons)
- If "Yes", show:
- Car Make (dropdown)
- Car Model (text input)
- Survey Question 2: How often do you drive? (dropdown: Daily, Weekly, Monthly, Rarely)
- Survey Question 3: What is your primary purpose of driving? (checkboxes: Work, Leisure,
Other)
- Submit button
2. HTML5 Features:
- Use `input` elements with `type="radio"` for Yes/No questions.
- Use `input` elements with `type="checkbox"` for multiple-choice questions.
- Use the `required` attribute for mandatory fields.
- Use `type="email"` for the email input.
3. CSS3 Features:
- Use flexbox or grid to layout the form.
- Style the form to be visually appealing with hover and focus effects.
- Use media queries to ensure the form is responsive.
- Add transitions to smoothly show/hide sections.
4. JavaScript (Optional):
- Add JavaScript to show/hide sections based on user input.
- Validate the form before submission.
Objective:
Create a contact form with custom styles and HTML5 validation features.
Requirements:
1. Form Structure:
- Name (text input)
- Email (email input)
- Subject (text input)
- Message (textarea)
- Submit button
2. HTML5 Features:
- Use the `required` attribute for mandatory fields.
- Use `type="email"` for the email input.
- Use `maxlength` and `minlength` attributes for the subject and message.
- Use the `pattern` attribute for the name field to allow only alphabets.
3. CSS3 Features:
- Style the form with custom colors, borders, and shadows.
- Implement hover and focus effects for inputs.
- Use CSS transitions to animate input focus states.
- Add media queries to ensure the form is responsive.
- Style validation messages using `:invalid` and `:valid` pseudo-classes.
- Create a custom submit button with hover effects.