Javascript pulls dates that appear to be random

I am new to JavaScript and I'm trying to create a webpage that asks the user "how many nights will you be staying?" The goal is to display something like this: "Total cost for checking in [today's date] and checking out after [number of nights stayed] nights will cost $[total price]."

However, I'm having trouble getting the correct date to display. It currently shows random dates instead of calculating x days from today. For example, if I enter 5 in the prompt, it displays "Total cost for checking in Thu Sep 24 2020 and checking out Wed Jan 01 0245 is $500." This is neither the current year nor is it 5 days from today.


var totalnights = prompt('How many nights will you be staying?');

var today;
var costMsg;
var nights;

function TotalCost(today) {
   
    nights = new Date(new Date().getDate() + (totalnights));
    nights = (nights.toDateString());
    
    today = new Date();
    today = (today.toDateString());

    var Msg = 'Total cost for checking in ' + today + ' and checking out ' + nights + ' is ' + '$' + hotel.roomRate * totalnights ;
    return Msg;
}

Answer №1

Your mistake lies in the date calculation process. The function new Date().getDate() only returns the current day of the month, whereas the necessary input parameters for Date should be referenced from this source.

To correct the code snippet above, you can implement the following solution:

let evening = new Date()
evening.setDate(evening.getDate() + totalnights)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing interconnected dynamic components in Angular

Can you help me figure out how to create nested dynamic components while maintaining the parent-child relationship? For instance, if I have data structured like this: - A --A.1 --A.2 -B --B.1 -C I want to build components that reflect this structure, su ...

The application of the Same Origin Policy appears to be ambiguous when utilizing the jQuery AJAX

I'm exploring the application of the Same Origin Policy (SOP) in various scenarios. Here is some JavaScript code that I have written in a local HTML file and executed using Chrome on Windows: $(document).ready(function () { $.get("http://www.qu ...

What is the best way to instantiate a dynamic object within a literal?

I am dealing with an object that contains multiple fields, mainly consisting of constant string values. However, I also need to incorporate a variable that is determined by the current state. const {order} = this.state; myObject={{ fieldA: 2, fiel ...

Is there a way to extract the ID or other attributes from a clicked element using AMP HTML and incorporate them to generate a URL for an anchor tag?

I have a collection of checkboxes, each with its own unique ID. When a checkbox is clicked, I need to extract the ID and generate a string like '/some/path/?myids=checkOne,checkTwo' (where checkOne and checkTwo are IDs of two different checkboxes ...

The iPhone numerical keyboard continues to appear even after submitting a form within an iFrame on iOS Safari

Our React web application incorporates a Stripe element form for the checkout process, which involves injecting an iFrame form onto the website. An issue arises when using iOS Safari – the credit card keyboard pops up when a user interacts with the Strip ...

An illustration of webpack 4 and Vue.js compatibility tailored specifically for Internet Explorer 11, featuring multiple entry points

This feels like déjà vu - yet another issue with Internet Explorer and webpack. I'm on the brink of deploying my project when IE 11 decides to mess everything up. I thought I had covered all bases with babel-polyfill and the latest versions, but of ...

Issue with Vue Checkbox not functioning properly upon being mounted in Edge browser

Currently, I am developing a Vue page with a checkbox that needs to read a value from a cookie upon mounting. However, I have observed that in the Edge browser, the checkbox does not get set correctly even though the Mounted function is definitely being ca ...

How can I add both the keys and values to an array in order to construct a dynamic query?

When building the query object for MongoDB, I encountered an issue with the output. Here is what I got: output {"$match":{"$and":[{id:1},{name:"Alfred"},{location:"moon"},{"values":{"$in":[{name: ...

Can WebdriverJS serve as a comprehensive substitute for the traditional selenium webdriver?

My quest for a Javascript-based web automation framework led me to discover WebdriverJS On their homepage, WebdriverJS boasts the following features: ✔ Offers over 50 useful actions for interacting with your application ✔ Supports selenium tests in ...

Centralizing images in a Facebook gallery/lightbox during the loading process

Is the image width and height stored in Facebook's gallery database? In typical JavaScript usage, the image width and height cannot be determined until the image is fully loaded. However, on Facebook, images are pre-loaded before being displayed, ens ...

Guide on integrating buefy (a vue.js component library) into your Laravel blade template

I'm currently integrating buefy into my project, but I'm encountering issues with using vue.js on Laravel 5.8. Can anyone offer assistance? Here is the code snippet from my app.js: require('./bootstrap'); window.Vue = require('v ...

Creating Interactive Lookup Field with JQuery and PHP

Recently, I followed a guide on how to develop a lookup field that dynamically searches a MySQL database table - the main objective is to facilitate contact search by postcode. While I managed to get the search functionality working and display the value o ...

AJAX causing issues with file upload functionality

I am currently utilizing AJAX in a file upload process, you can view it here. However, I am facing an issue where the value is not being passed from one page (upload.php) to another page (file_upload_submit.php). I am unsure of how to retrieve the file val ...

Issue with TextAngular not properly applying text alignment functionality

I've been incorporating TextAngular into a website I'm developing, and overall it's been working well except for a couple of issues. text-align: center is not functioning. text-align: right is also not working. I came across some similar ...

identify which specific key has been activated

What is the best way to identify which key has been pressed using javascript? ...

Does JavaScript return null or empty values for all DOM manipulating functions?

Currently diving into the realm of JavaScript with the help of "Professional Javascript for Web Developers," but running into a roadblock when attempting to manipulate the DOM tree. Testing out an example locally, which works perfectly fine on jsfiddle: ...

Anchor element not displaying Bootstrap popover upon focus trigger

I'm exploring ways to implement bootstrap popovers that respond to both click and hover events without relying on jQuery. I have a variety of controls on my page and I want to bind popovers to them using JavaScript. While testing with buttons and anc ...

Having trouble retrieving the data property from the parent component within the child component slot

I am facing an issue with my Vue components. I have a rad-list component and a rad-card component. In the rad-list component, I have placed a slot element where I intend to place instances of rad-card. The rad-card component needs to receive objects from t ...

Several iterators failing to function correctly within a Java for loop

Attempting to transfer a Javascript code to Java has presented a challenge with the for loop. The Javascript code contains a for loop with 2 iterators that functions correctly, but when translated to Java, it encounters issues. The problem lies in Java whe ...

The function is not functioning correctly for every element within the array

I am currently developing a shopping cart that populates data from local storage. Here is a snippet of the code: var arrayLength = cartarry.length; for (var i = 0; i < arrayLength; i++) { var id = cartarry[i].id; var name = cartarry[i].name; ...