Run npm init and follow the prompts to create a package.json file
Install Express and ejs using the following command: npm install express ejs
Step 2: Create the server
Create a new file called server.js
Import Express and create a new Express app
Set up a static folder to serve static assets (e.g. CSS, images)
Set up the view engine to ejs
Define a route to serve the home page
Listen for incoming requests on a port of your choice
Server.js
constexpress=require('express')constapp=express()constPORT=3000// View engineapp.set('view engine','ejs')app.use(express.static('public'))// Basic routingapp.get('/',(req,res)=>{res.render('pages/index.ejs')})// Starting serverapp.listen(PORT,()=>{console.log(`The server is running on port ${PORT}, You better go catch it!`)})
Step 3: Create the HTML and CSS
Create a new file called index.ejs in a new folder called views
Add the HTML for the form to collect the bill amount, tip percentage, and number of people
Add a button to submit the form
Add the HTML elements to display the results of the calculation
Create a new file called style.scss in a new folder called public/css
Write the CSS to style the form and the results
In index.ejs
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Splitwise</title><linkrel="stylesheet"href="/css/style.css"></head><body><h1>Splitwise</h1><formid="tip-form"><div><labelfor="bill-amount">Bill amount</label><inputtype="number"id="bill-amount"name="bill-amount"required></div><div><labelfor="tip-percentage">Tip percentage</label><inputtype="number"id="tip-percentage"name="tip-percentage"required></div><div><labelfor="num-of-people">Number of people</label><inputtype="number"id="num-of-people"name="num-of-people"required></div><buttontype="submit">Calculate</button></form><divid="results"><div><label>Total amount:</label><spanid="total-amount"></span></div><div><label>Amount per person:</label><spanid="amount-per-person"></span></div></div><script src="/js/main.js"></script></body></html>`
Get the form and the results container elements from the HTML document.
Add an event listener to the form submit button that triggers a function when the button is clicked.
In the function, prevent the default form submission behavior so the page doesn't reload when the button is clicked.
Get the values of the bill amount, tip percentage, and number of people inputs from the form using querySelector and parseFloat or parseInt.
Calculate the total tip amount by multiplying the bill amount by the tip percentage divided by 100.
Calculate the total bill amount by adding the bill amount and the tip amount together.
Calculate the amount per person by dividing the total bill amount by the number of people.
Update the results container HTML with the calculated amounts using innerHTML and template literals (backticks).
Display the results to the user on the screen.
// Get the form and the results container constform=document.querySelector('form');constresultsContainer=document.querySelector('#results');// Add an event listener to the form submit button form.addEventListener('submit',(event)=>{// Prevent the default form submission behavior event.preventDefault();// Get the form inputs constbillAmount=parseFloat(document.querySelector('#bill-amount').value);consttipPercentage=parseFloat(document.querySelector('#tip-percentage').value);constnumberOfPeople=parseInt(document.querySelector('#number-of-people').value);// Calculate the total tip amount and the total bill amount consttipAmount=billAmount*(tipPercentage/100);consttotalAmount=billAmount+tipAmount;// Calculate the amount per person constamountPerPerson=totalAmount/numberOfPeople;// Display the results resultsContainer.innerHTML=`
<div>
<label>Total Amount:</label>
<span>$${totalAmount.toFixed(2)}</span>
</div>
<div>
<label>Total Tip:</label>
<span>$${tipAmount.toFixed(2)}</span>
</div>
<div>
<label>Amount Per Person:</label>
<span>$${amountPerPerson.toFixed(2)}</span>
</div> `;});
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)