HTML javascript movie and dinner - javascript

I'm receiving NotANumber for my total from movieTotal, as well as im not getting the correct value of adding pricePerTicket + pricePerDinner. Can someone help me figure out what im doing wrong?
<script type="text/javascript">
<!--
var ticket, earlyBirdTicket, WeekDinner, weekendDinner, numberOfTickets, TotalDue;
var numberOfTickets, pricePerTicket, pricePerDinner, costOfDandT, totalAmountOwed;
var totalDandT, yes, week, movieTotal;
var ticket = 5;
var nightTicket = 10;
var weekDinner = 8;
var weekendDinner = 12;
var yes = ticket;
var week = weekDinner;
var movieTotal = totalDandT * numberOfTickets;
totalDandT = pricePerDinner + pricePerTicket;
numberOfTickets = prompt ("How many tickets?");
pricePerTicket = prompt ("Is this earlybird? yes/no ");
pricePerDinner = prompt ("weekend or weekday? week/weekend ");
pricePerTicket = parseInt(pricePerTicket);
pricePerDinner = parseInt(pricePerDinner);
movieTotal = parseInt (movieTotal);
if (pricePerTicket = yes)
{
pricePerTicket = ticket;
}
else
{
pricePerTicket = nightTicket;
}
if (pricePerDinner = week)
{
pricePerDinner = weekDinner;
}
else
{
pricePerDinner = weekendDinner;
}
document.write ("<br>Number of tickets sold : " + numberOfTickets);
document.write ("<br>Cost per ticket tonight : $" + pricePerTicket);
document.write ("<br>Cost per dinner tonight : $" + pricePerDinner);
document.write ("<br>Cost of dinner and ticket : $" + pricePerTicket + pricePerDinner);
document.write ("<br> Your total today is $" + movieTotal);
// -->
</script>

You have not initialized the variables. You should do it before using them.
Uninitialized variables are equals to undefined so yes.undefined plus undefined is not a number.

Related

"undefined" error prints out in Javascript

Good day,
I'm coding a simple calculation price based on:
A single price
The frequency of that price (Weekly, daily, etc.).
I keep having the word "undefined" being printed out in the page. I've tried to redefine all the variable of that part. All variables are correctly defined. Do you guys see any error in my code below?
//parking cost
var parking_cost1 = 0;
var parkingCostUnity1 = 0;
var parkingCostFrequency1 = 0;
parking_cost1 = new Number(document.getElementById("parking_cost1").value);
parkingCostFrequency1 = document.getElementById("parking_cost1_frequence1").value;
if (parkingCostFrequency1 == "per_night") {
parkingCostUnity1 = result;
}
if (parkingCostFrequency1 == "per_week") {
parkingCostUnity1 = nrOfWeeks;
}
if (parkingCostFrequency1 == "per_month") {
parkingCostUnity1 = nrOfMonths;
}
var echoParkingCostFrequency1 = parkingCostFrequency1.replace("_", " ");
var totalParkingCost1 = 0;
totalParkingCost1 = parkingCostUnity1 * parking_cost1;
if (totalParkingCost1 > 0) {
var printOutTotalParkingCost1 = '<tr><td><b>Total parking cost</b></td><td>' + parking_cost1 + ' ' + currency + ' ' + echoParkingCostFrequency1 + '</td><td>' + totalParkingCost1 + ' ' + currency + '</td></tr>';
}
<i class="fas fa-parking"></i> <text class="form_basic_text" />Parking cost<br>
<input type="number" onChange="resume()" name="parking_cost1" id="parking_cost1" class="form_basic_field_bid" style="width:auto;" /><br/>
<center>
<select onChange="resume()" name="parking_cost1_frequence1" id="parking_cost1_frequence1" style="width:auto;height:25px;padding:0px;width:auto;">
<option value="per_night">Per night</option>
<option value="per_week">Per week</option>
<option value="per_month">Per month</option>
</select>
</center>
The following variables have already been defined in the document:
//var currency
var currency = document.getElementById("currency").value;
//This variable is always defined, as it's a dropdown.
//var result, nrOfWeeks and nrOfMonths are all under the same parameters
//check-in date
var checkInDate = new Date(document.getElementById("check_in_date").value);
var checkInDateResume = checkInDate.getDate() + '-' + (checkInDate.getMonth()+1) + '-' + checkInDate.getFullYear();
if(checkInDateResume=="NaN-NaN-NaN"){
checkInDateResume = "<text class='alert'>Not defined yet</text>";
result = 1;
nrOfWeeks = 1;
nrBiWeeks = 1;
nrOfMonths = 1;
}
var checkOutDate = new Date(document.getElementById("check_out_date").value);
var checkOutDateResume = checkOutDate.getDate() + '-' + (checkOutDate.getMonth()+1) + '-' + checkOutDate.getFullYear();
if(checkOutDateResume=="NaN-NaN-NaN"){
checkOutDateResume = "<text class='alert'>Not defined yet</text>";
result = 1;
nrOfWeeks = 1;
nrBiWeeks = 1;
nrOfMonths = 1;
}
//Since the nr of months, week and result (the number of nights) is the difference between the check-in and check-out date - Those aren't defined except as mentionned up here. We then define the rest of the vars
var difference = Math.abs(checkOutDate-checkInDate);
var result = difference/(1000*3600*24);
var nrOfMonths = Math.ceil(result/30.41);
var nrOfWeeks = Math.ceil(result/7);
Thank you!
Edit: It's my first time posting here. Thank you for your patience and your comments on how to make my question more understandable!
I did some extended research and discovered my mistake:
I forgot to add an else to the the last IF statement.
if(totalParkingCost1>0){
var printOutTotalParkingCost1 = 'XYZ';
}
The var printOutTotalParkingCost1 was used in any scenario. I added the following else
else{
var printOutTotalParkingCost1 = "";
}
Now, the variable printOutTotalParkingCost1 is defined whatever the scenario is. Thank you all for your kind help! Thanks to #ivar for his extensive explanations.

Resolve 'Parsing Error: Please check your selector. (line XX)' Javascript/AWQL

First off, let me say that I am not a developer, nor do I really code beyond basic HTML. So I appreciate your patience. :)
I'm working with a script that is for AdWords, but I believe it's more or less written in Javascript. (I've included the script below.)
Basically, I'm receiving the error message 'Parsing Error: Please check your selector. (line XX)' when I preview the script.
I've searched all around for hours and have yet to find a solution.
I think it may be that a query being returned contains either a single or double quote, and may be messing up the code? Though I can't actually prove that.
Also, yes, I was sure to update lines 17-21 with the correct details.
Any help would be much appreciated!
Thanks!
John
/*
// AdWords Script: Put Data From AdWords Report In Google Sheets
// --------------------------------------------------------------
// Copyright 2017 Optmyzr Inc., All Rights Reserved
//
// This script takes a Google spreadsheet as input. Based on the column headers, data filters, and date range specified
// on this sheet, it will generate different reports.
//
// The goal is to let users create custom automatic reports with AdWords data that they can then include in an automated reporting
// tool like the one offered by Optmyzr.
//
//
// For more PPC management tools, visit www.optmyzr.com
//
*/
var DEBUG = 0; // set to 1 to get more details about what the script does while it runs; default = 0
var REPORT_SHEET_NAME = "report"; // the name of the tab where the report data should go
var SETTINGS_SHEET_NAME = "settings"; // the name of the tab where the filters and date range are specified
var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/1dttJTb547L81XYKdTQ56LcfO9hHhbb9wm06ZY5mKhEo/edit#gid=0"; // The URL to the Google spreadsheet with your report template
var EMAIL_ADDRESSES = "example#example.com"; // Get notified by email at this address when a new report is ready
function main() {
var currentSetting = new Object();
currentSetting.ss = SPREADSHEET_URL;
// Read Settings Sheet
var settingsSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(SETTINGS_SHEET_NAME);
var rows = settingsSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
var sortString = "";
var filters = new Array();
for(var i = 0; i < numRows; i++) {
var row = values[i];
var settingName = row[0];
var settingOperator = row[1];
var settingValue = row[2];
var dataType = row[3];
debug(settingName + " " + settingOperator + " " + settingValue);
if(settingName.toLowerCase().indexOf("report type") != -1) {
var reportType = settingValue;
} else if(settingName.toLowerCase().indexOf("date range") != -1) {
var dateRange = settingValue;
} else if(settingName.toLowerCase().indexOf("sort order") != -1) {
var sortDirection = dataType || "DESC";
if(settingValue) var sortString = "ORDER BY " + settingValue + " " + sortDirection;
var sortColumnIndex = 1;
}else {
if(settingOperator && settingValue) {
if(dataType.toLowerCase().indexOf("long") != -1 || dataType.toLowerCase().indexOf("double") != -1 || dataType.toLowerCase().indexOf("money") != -1 || dataType.toLowerCase().indexOf("integer") != -1) {
var filter = settingName + " " + settingOperator + " " + settingValue;
} else {
if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + ' "' + settingValue + '"';
} else if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
} else {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
}
}
debug("filter: " + filter)
filters.push(filter);
}
}
}
// Process the report sheet and fill in the data
var reportSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(REPORT_SHEET_NAME);
var rows = reportSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
// Read Header Row and match names to settings
var headerNames = new Array();
var row = values[0];
for(var i = 0; i < numCols; i++) {
var value = row[i];
headerNames.push(value);
//debug(value);
}
if(reportType.toLowerCase().indexOf("performance") != -1) {
var dateString = ' DURING ' + dateRange;
} else {
var dateString = "";
}
if(filters.length) {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + ' WHERE ' + filters.join(" AND ") + dateString + " " + sortString;
} else {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + dateString + " " + sortString;
}
debug(query);
var report = AdWordsApp.report(query); //THIS IS LINE 103 WITH THE ERROR
try {
report.exportToSheet(reportSheet);
var subject = "Your " + reportType + " for " + dateRange + " for " + AdWordsApp.currentAccount().getName() + " is ready";
var body = "currentSetting.ss<br>You can now add this data to <a href='https://www.optmyzr.com'>Optmyzr</a> or another reporting system.";
MailApp.sendEmail(EMAIL_ADDRESSES, subject, body);
Logger.log("Your report is ready at " + currentSetting.ss);
Logger.log("You can include this in your scheduled Optmyzr reports or another reporting tool.");
} catch (e) {
debug("error: " + e);
}
}
function debug(text) {
if(DEBUG) Logger.log(text);
}
The area between SELECT and FROM is the selector. You're not selecting any fields with that query. That's happening because the headerNames array is empty. Verify the value of REPORT_SHEET_NAME

JavaScript - is there a way to enable multiple entries from one prompt box?

I'm trying to make a program where the user can enter how many CD's they wish to enter and then print out what they have entered onto a screen.
Is there a way to enable multiple entries from one prompt box?
<script language="JavaScript">
var titleCD = [];
var costCD = [];
var subTotal = [];
var entries = parseInt(prompt("How many CDs do you wish to enter?",0));
while(entries>0) {
titleCD = prompt("What is the title of your CD?");
costCD = parseInt(prompt("What is the cost of that CD?", 0));
entries--;
}
b = new String(titleCD + ": " + "£" + costCD + "<br>");
document.write(b.bold());
u = new String("The subtotal cost of all CDs is: £" + subTotal);
document.write(u.toUpperCase());
</script>

How can I keep this loop going until balance is zero?

I am new to Javascript and have figured how to make the first line log to the console, however, how can I continue the loop and display the results as the balance gets lower until the balance is zero? I appreciate any help with this. Thanks in advance.
function displayPayment() {
var year = 0;
var balance = 1500;
var interest = 0.015;
var minimum = 0.02;
var payNum = 0;
if (balance > 0) {
var newPayNum = payNum + 1;
var newYear = year + 1;
var interestPaid = balance * interest;
var newBalance = interestPaid + balance;
var minPay = newBalance * minimum;
var balanceOwe = newBalance - minPay;
return (" " + newYear + " " + balanceOwe + " " + newPayNum + "
" + interestPaid);
}
}
console.log(" Year " + " Balance " + " PaymentNum " + " InterestPaid \n");
console.log(displayPayment());
You can use either the do while loop or just a while loop. However your balance does not change during the process, so the loop will never exit.

Basic Javascript Program, help need

My program needs to consist of a for loop, use both input functions already created ( name and number) and totals need to be acquired. I also need to be able to CANCEL and go to my doc.write where i would input name and number. I also need to give the user another chance to type in their name or number if they accidentally typed in number where letters should be and vise-versa. I think i have majority of the structure right, any help would be greatly appreciated!
function inputName() {
var nameIn = prompt("Enter your Name");
while(!isNaN(nameIn)) {
nameIn = prompt("Must contain only letters");
}
return nameIn;
}
/* INPUT NUMBER */
function inputNum(){
var numIn=parseFloat(prompt("Enter the number of hours worked \n 0-60 hours"));
var min=0;
var max=60;
while(numIn>min && numIn>max ){
numIn=prompt("Enter a valid number between 0-60");
return numIn;
}
</script>
<body>
<script type="text/javascript">
//DECLARATIONS
var wage=10.00;
var earned; // hrsWrked*wage
var totHrsWrked=0;
var totEarning=0;
var BR= "
";
var howMany;
var loopControl;
//INPUT & PROCESSING
howMany=parseFloat(prompt("How many employees are you inputing?"));
for(loopControl=1 ; loopControl <= howMany; ++loopControl){
var inpNam=inputName();
var inpNumber=inputNum();
earned= inpNumber*wage;
totEarning+=earned;
totHrsWrked+=inpNumber;
//OUTPUT
document.write("Name: "+ inpNam+ BR);
document.write("Hours Worked: " + inpNumber + BR);
document.write("Money Earned: $ " + earned + BR +BR);
}
document.write("Total Hours Worked: " + totHrsWrked.toFixed(2) + BR);
document.write("Total Earnings: " + "$"+totEarning.toFixed(2)+ BR+BR);
</script>
</body>
</html>
Here's the edited version of your code. :)
<! doctype html>
<html>
<body>
<script>
//DECLARATIONS
var wage=10.00, earned, totHrsWrked=0, totEarning=0, howMany;
//INPUT & PROCESSING
howMany=parseFloat(prompt("No of employees"));
for( var loopControl=1 ; loopControl <= howMany; ++loopControl)
{
var inpNam=inputName();
var inpNumber=inputNum();
earned= inpNumber*wage;
totEarning+= (+earned);
totHrsWrked+= (+inpNumber);
//OUTPUT
document.write("Name: "+ inpNam+ "<br>");
document.write("\n Hours Worked: " + inpNumber + "<br>");
document.write("Money Earned: $ " + earned + "<br><br>");
}
document.write("Total Hours Worked: " + totHrsWrked.toFixed(2)+ "<br>");
document.write("Total Earnings: " + "$"+totEarning.toFixed(2)+ "<br>");
//INPUT NAME
function inputName() {
var nameIn = prompt("Enter your Name");
while(!isNaN(nameIn)) {
nameIn = prompt("Must contain only letters");
}
return nameIn;
}
//INPUT NUMBER
function inputNum(){
var numIn=parseFloat(prompt("Enter the number of hours worked \n 0-60 hours"));
var min=0;
var max=60;
while(numIn<=min || numIn>=max ){
numIn=prompt("Enter a valid number between 0-60");
}
return numIn;
}
</script>
</body>
</html>

Categories

Resources