Im building mini web application that on the first VIEW Im receiving "Starting Date" and "Second Date". When I click button "Rent It" to calculate the price based on the given dates. I have 4 special periods that are using different prices.
Example: Im receiving dates between 10th and 13th of January 2020. This is 4 days of the fourthPeriod so the price should be 4*150= 600 totalPrice. If its out of these periods, there is defaultPrice which is 5. And also to display the addedDate if its between the given periods. Now im not receiving nothing, something is really wrong and Im stuck. Thanks!
This is the input

This should be the output

I made that logic (I hope its right). There are comments for understanding.
//TODO: Fire function on load and get the start date, the end date and make the array
function getTotalPrice(arrPeriods, rentDateStart, rentDateEnd, defaultPriceOfDay) {
let totalPrice = 0;
let defaultPrice = 5;
let startOfPeriod = new Date();
let endOfPeriod = new Date(startOfPeriod.getTime() + (48 * 60 * 60 * 1000));
let month = startOfPeriod.getMonth();
let daysCountOfPeriod = [];
let day = 1000*60*60*24;
let diff = (endOfPeriod.getTime() - startOfPeriod.getTime()) / day;
//TODO: Calculate all separate days from start date to end date and put them in an array
for ( let i = 0 ; i <= diff; i++)
{
let a = startOfPeriod.getTime() + day * i;
let b = new Date(a);
let c = (b.getDate() + "-" + (b.getMonth() + 1) + "-" + b.getFullYear());
let separator = c.split('-');
let cDate = new Date(separator[2], separator[1] - 1, separator[0]);
daysCountOfPeriod.push(cDate)
}
//TODO: Declare the period of the different periods and the daily cost rate during this period
let firstPeriod = {startDate: new Date(2020,1-1,1), endDate: new Date(2020,1-1,4), addingDate: new Date(2019, 6, 1), pricePerDay: 2};
let secondPeriod = {startDate: new Date(2020,1-1,3), endDate: new Date(2020,1-1,8), addingDate: new Date(2019, 6, 2), pricePerDay: 60};
let thirdPeriod = {startDate: new Date(2020,1-1,5), endDate: new Date(2020,1-1,6), addingDate: new Date(2019, 6, 1), pricePerDay: 15};
let fourthPeriod = {startDate: new Date(2020,1-1,8), endDate: new Date(2020,1-1,15), addingDate: new Date(2019, 6, 15), pricePerDay: 150};
//TODO: Making an array with all the given periods
let allPeriods = [firstPeriod, secondPeriod, thirdPeriod, fourthPeriod]
//TODO: Checks how many dates there are in the array and start the loop
for (let u = 0; u < daysCountOfPeriod.length; u++) {
for(let j = 0; j < allPeriods.length; j++) {
if (daysCountOfPeriod[u] >= allPeriods[j].startDate && daysCountOfPeriod[u] <= allPeriods[j].endDate) {
//TODO: Add costRate of this date to totalPrice
totalPrice = totalPrice + allPeriods[j].pricePerDay;
}
else {
totalPrice = totalPrice + defaultPrice;
}
}
}
//TODO: Return the total price
return totalPrice;
}
Now I want to implement this to the project using EJS. Thats is what have I done.
SERVER.JS - Here is the server logic
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"))
let daysCountOfPeriod = [];
//TODO: Get methods
app.get("/result", function (req, res){
res.render("result", {
daysCountOfPeriod: daysCountOfPeriod
});
});
app.get("/", function (req, res){
res.render("dates")
})
//TODO: Post methods
app.post("/", function (req, res){
const gettingDates = {
startingDate: req.body.start,
endingDate: req.body.end
};
daysCountOfPeriod.push(gettingDates);
res.redirect("/result")
});
//TODO: Shows that our server is running at localhost:3000
app.listen(3000, function (){
console.log("Server is running!")
});
This is dates.ejs basic html:
<main class="form-signin">
<form action="/" method="post">
<img class="mb-4" src="images/logo.png" alt="" width=150 height="150">
<div class="form-floating">
<input type="date" class="form-control top" name="start" placeholder="Starting Date">
<label for="floatingInput">Starting Date</label>
</div>
<div class="form-floating">
<input type="date" class="form-control bottom" name="end" placeholder="Ending Date">
<label for="floatingPassword">Ending Date</label>
</div>
<button class="w-100 btn btn-lg btn-warning" type="submit" name="button">Rent It!</button>
<p class="mt-5 mb-3 text-muted">© Bike rental company</p>
</form>
</main>
</body>
And lastly the result.ejs
<body class="text-center">
<main class="form-signin">
<section>
<img class="mb-4" src="images/logo.png" alt="" width=150 height="150">
<% let totalPrice = 0; %>
<% let added = ""; %>
<% let firstPeriod = {startDate: new Date(2020,1-1,1), endDate: new Date(2020,1-1,4), addingDate: new Date(2019, 6, 1), pricePerDay: 2}; %>
<% let secondPeriod = {startDate: new Date(2020,1-1,3), endDate: new Date(2020,1-1,8), addingDate: new Date(2019, 6, 2), pricePerDay: 60}; %>
<% let thirdPeriod = {startDate: new Date(2020,1-1,5), endDate: new Date(2020,1-1,6), addingDate: new Date(2019, 6, 1), pricePerDay: 15}; %>
<% let fourthPeriod = {startDate: new Date(2020,1-1,8), endDate: new Date(2020,1-1,15), addingDate: new Date(2019, 6, 15), pricePerDay: 150}; %>
<% let allPeriods = [firstPeriod, secondPeriod, thirdPeriod, fourthPeriod] %>
<% for (let u = 0; u < daysCountOfPeriod.length; u++) { %>
<% for(let j = 0; j < allPeriods.length; j++) { %>
<% if (daysCountOfPeriod[u] >= allPeriods[j].startDate && daysCountOfPeriod[u] <= allPeriods[j].endDate) { %>
//Add price of this date to totalPrice %>
<% totalPrice = totalPrice + allPeriods[j].pricePerDay; %>
<% added = allPeriods[j].addingDate; %>
<% } %>
<% } %>
<% } %>
<h1>Total Price</h1>
<h3><%= totalPrice %>$</h3>
<h3>Added on: <%= added %></h3>
</section>
</main>
</body>
I want to take the date "daysCountOfPeriod" into the inputs, make it to the array and then loop it and check it for the given period and to get a proper result, now im not getting nothing. Im a beginner , so I will need some help.
Example: Im receiving dates between 10th and 13th of January 2020. This is 4 days of the fourthPeriod so the price should be 4*150= 600 totalPrice. If its out of these periods, there is defaultPrice which is 5. And also to display the addedDate if its between the given periods. Now im not receiving nothing, something is really wrong and Im stuck. Thanks!
Aucun commentaire:
Enregistrer un commentaire