Assignment Title: JavaScript Operations and Data Types
Instructions: In this assignment, you will work with various JavaScript operations and data types. Follow the instructions for each task and submit your completed JavaScript code.
Certainly! Here are three JavaScript questions that focus on explicit type conversion:
Question 1: Convert to Number Explicitly (15 points)
Write a JavaScript program that demonstrates explicit type conversion to a number. Perform the following steps:
- Declare a variable
numberStr
and assign it a string value, e.g.,"42"
. - Use explicit type conversion to convert
numberStr
to a number using theNumber()
function. - Display the converted number in the console.
Explain in comments how you explicitly converted the string to a number using the Number()
function.
Question 2: Convert to String Explicitly (15 points)
Create a JavaScript program that showcases explicit type conversion to a string. Follow these instructions:
- Declare a variable
num
and assign it a numerical value, e.g.,2023
. - Use explicit type conversion to convert
num
to a string using theString()
function. - Display the converted string in the console.
Provide comments to explain how you explicitly converted the number to a string using the String()
function.
Question 3: Convert to Boolean Explicitly (15 points)
Write a JavaScript program that illustrates explicit type conversion to a boolean. Complete the following steps:
- Declare a variable
truthyValue
and assign it a truthy value, e.g.,"Hello"
. - Declare a variable
falsyValue
and assign it a falsy value, e.g.,0
. - Use explicit type conversion to convert
truthyValue
to a boolean using theBoolean()
function. - Use explicit type conversion to convert
falsyValue
to a boolean using theBoolean()
function. - Display the converted boolean values in the console.
Here are two additional JavaScript questions that focus on working with dates:
Extra questions for practice(not required)
Question 4: Convert JavaScript Dates to Numbers (20 points)
Write a JavaScript program that demonstrates how to convert JavaScript dates to numbers. Perform the following steps:
- Declare a variable
currentDate
and assign it the current date and time using thenew Date()
constructor. - Use the
getTime()
method to convertcurrentDate
to a number representing the number of milliseconds since January 1, 1970 (UNIX epoch time). - Display the converted number in the console.
Include comments to explain how you converted the JavaScript date to a number using the getTime()
method.
Question 5: Convert JavaScript Dates to Strings (20 points)
Create a JavaScript program that shows how to convert JavaScript dates to strings. Follow these instructions:
- Declare a variable
eventDate
and assign it a specific future date and time using thenew Date()
constructor. - Use the
toLocaleString()
method to converteventDate
to a localized date and time string. - Display the converted string in the console.
Provide comments to explain how you converted the JavaScript date to a localized string using the toLocaleString()
method.
This addition to the assignment will help students understand how JavaScript handles data type conversions automatically and how they can control conversions manually when working with different data types. Submission Guidelines:
- Create a single JavaScript file (e.g.,
assignment.js
) to complete all tasks. - Include comments to explain your code where necessary.
- Test your code thoroughly and ensure it works as expected use console.log.
- Submit your JavaScript file along with any necessary instructions or comments.
Grading Criteria: Your assignment will be graded based on correctness, completeness, code organization, and adherence to the instructions.
Note: Remember to test your code thoroughly and ensure it runs without errors before submitting your assignment.
Important: Fork this repository and commit your assignment code to your forked repository. Submit the URL to Evelyne after you are done with the assignment.
Happy Coding! 😄