JavaScript
Basic Syntax
Variables
Data types
Numbers
Basic IO
Conditionals
Nested conditions
Git branches
GUI Tools for Git
Code style: Basics
Introduction to AI
Practice
Assignment
Back to core program
Week 2 Assignment
Task 1 - Leap Year Check
A leap year has 366 days instead of 365, with February receiving an extra day. Here are the rules for determining a leap year:
- A year is a leap year if it is divisible by 4
- However, if the year is divisible by 100, it is not a leap year
- Unless the year is also divisible by 400, in which case it is a leap year
Examples:
- The year 2028 is a leap year because it is divisible by 4
- The year 1900 is not a leap year because it is divisible by 100
- The year 2000 is a leap year because it is divisible by 400
- The year 2025 is not a leap year because it is not divisible by 4
Requirements
Write a JavaScript application that gets a valid year from a prompt and prints one of the following outputs:
- "Yes, <YEAR> is a leap year"
- "No, <YEAR> is not a leap year"
- “Invalid year!”
Replace <YEAR> with the year provided by the user.
A Valid year is between 1 and 9999.
Additional Instructions
- You can find the starter code in the
task-1 folder. Implement your logic in leap-year.js.
- Don’t forget to run
npm install to download the prompt-sync library.
- You will need to convert the user input to a Number type.
Task 2 - Login screen
In this task, you are asked to write the logic for a login page in an application. Our designers created this nice looking login screen:

In the task-2 folder, you will find multiple files. The only file you will be working on is login.js. You can ignore the rest of the files as they only support the design and basic events of the login screen.
Requirements
- On a correct username and password combination, show a success message “Logged in successfully”
- On a wrong username and password combination, show an error message “Incorrect credentials”.
- If the user inputed the wrong password for more than 3 times (4 or more), block the login page.
- If the login place is blocked, show an error message “Login blocked: Too many incorrect attempts” regardless if the credentials are correct or not
The applications has two valid users:
| Username |
Password |
| admin |
Hack1234 |
| user |
7654321 |
Additional Instructions
- Write your logic inside
login.js where it says "Write your code here". Your code will go inside the onlogin function. Don't worry, you don't need to understand what a function is, we will learn about functions next week. Just note that you can use the username and password variables to read the user's input.
- Use
errorMessage('message') to display error messages and successMessage('message') to display success messages.
<aside>
💡
You can run the code using the recommended Live Server VSCode plugin.
</aside>
Task 3 - Currency converter app
You have been assigned to fix a couple of bugs in a currency convert app that was built by another developer. In task-3 you will find a file called converter.js that contains the code of the application. The application should work in the following way:
- Ask the user to choose 1 or 2 to select which currency they want to convert to (the target currency).
- Reject any other options with a message.
- Ask for the amount in the base currency
- Reject invalid non-numeric or negative amounts
- Calculate and display the amount in the target currency.
Here is an example input of the working application
Hello and welcome to the currency converter. Please choose:
1: Convert EUR to USD
2: Convert USD to EUR
Select your option [1 or 2]: 1
Enter amount in EUR: 150
150.00 EUR is equal to 174.64 USD.
Requirements
- Fix 3 bugs (see below)
- Implement one feature (see below)
Instructions
- Run
npm install in the task-3 folder to download the prompt-sync package
- Open
converter.js and inspect the code to get the idea how it works.
Bugs to Fix
Unfortunately, the code has some issues, and it's up to you to find and fix these bugs:
- The application won't run at all. There's an error in the console when you try to run it. Check the console and try to understand what's wrong. VSCode can also give you a hint.
- After fixing bug 1, the application runs, but the conversion from USD to EUR fails with a new error message in the console. Investigate this error by reading the console message carefully.
- The conversion from EUR to USD isn't working either. We always get the message "Please enter a valid positive number for the amount" when we type a valid amount like 100. There's no console error here because this is a logical error in the application. Review the code for the EUR to USD conversion and see if you can spot the issue.
Feature to implement
Add a third option (number 3) to display the current exchange rate. The output should look like this:
Hello and welcome to the currency converter. Please choose:
1: Convert EUR to USD
2: Convert USD to EUR
3: Display the current exchange rate
Select your option [1, 2, or 3]: 3
The current exchange rate is 1 EUR = 1.1643 USD.