### how can I correct this JavaScript adding function?

```I have a form which takes six inputs (there are more but only these matter for now)
original fare
original tax
new fare
new tax
fee
number of guests
when "Calculate" is pressed I use javascript to add original fare
and original tax to get the original total
then I add new fare and new tax to get the new total
now I compare original total to new total
if original total is greater than new total it should use one if several methods to finish doing the math I need and set the results to the display
i was originally testing everything using these values:
original fare = 1000
original tax = 200
new fare = 800
new tax = 200
fee = 150
number of guests = 3
which can bee seen here https://pnrbuilder.com/_popups/exchangeMyPNR_MATH_TEST_2.php
(page only works in chrome)
the above works exactly as expected but when I change the values to:
original fare = 949.83
original tax = 321.18
new fare = 453.91
new tax = 143.91
fee = 150
number of guests = 3
seen here https://pnrbuilder.com/_popups/exchangeMyPNR_MATH_TEST.php
(page only works in chrome)
this test uses the wrong if statement to finish out the rest of the math
I dont understand why this is happening as original total is still > new total so it should use the same method as the first example. I put in alerts to let me know exactly which if statement is being used to do the math and clearly the wrong one is used here but I just cant figure out why.
I know this is convoluted but could someone please help me figure out where my logic goes wrong?
Here's the full function:
var orBase = Number(frm.box1.value);
var orTax = Number(frm.box2.value);
var nwBase = Number(frm.box3.value);
var nwTax = Number(frm.box4.value);
var fee = Number(frm.fee.value);
var gsts = Number(frm.guests.value);
var fltrd_orBase = orBase * 100;
var fltrd_orTax = orTax * 100;
var fltrd_orTtl = fltrd_orBase + fltrd_orTax;
var orTtl = fltrd_orTtl / 100;
var final_orTtl = orTtl.toFixed(2)
frm.result.value = orTtl.toFixed(2)
var fltrd_nwBase = nwBase * 100;
var fltrd_nwTax = nwTax * 100;
var fltrd_nwTtl = fltrd_nwBase + fltrd_nwTax;
var nwTtl = fltrd_nwTtl / 100;
var final_nwTtl = nwTtl.toFixed(2)
frm.result2.value = nwTtl.toFixed(2)
var e = document.getElementById("residual");
var selectVal = e.options[e.selectedIndex].value;
if (final_orTtl <= final_nwTtl) {
document.getElementById("forfeitTable").style.display="none";
document.getElementById("MCOtable").style.display="none";
var undiff = final_nwTtl - final_orTtl;
var diff =undiff
document.getElementById("differenceDisplay").innerHTML=diff.toFixed(2);
frm.difference.value = diff.toFixed(2);
var ppCost = diff + fee;
frm.pptotal.value = ppCost.toFixed(2);
document.getElementById("ppDisplay").innerHTML=ppCost.toFixed(2);
var ttlCost = ppCost * gsts;
frm.totalcost.value = ttlCost.toFixed(2);
document.getElementById("grandTotalDisplay").innerHTML=ttlCost.toFixed(2);
// this is just for testing to show which method was actually used
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
// this is in its own if statement so it doesnt popuop while entering data
}
}
else if (final_orTtl > final_nwTtl) {
if (selectVal == "residualX" || selectVal == "residualN") {
document.getElementById("MCOtable").style.display="none";
var diff = final_orTtl - final_nwTtl;
var displayDiff = diff* -1;
document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
frm.difference.value = displayDiff.toFixed(2);
document.getElementById("forfeitInfo").innerHTML = "Beyond the cost above";
frm.lost.value = diff.toFixed(2);
document.getElementById("ppForfeitedDisplay").innerHTML = diff.toFixed(2);
var ttlfForfeited = diff * gsts;
frm.lostTTL.value = ttlfForfeited.toFixed(2);
document.getElementById("totalForfeitedDisplay").innerHTML=ttlfForfeited.toFixed(2);
var ppCost = fee;
frm.pptotal.value = ppCost.toFixed(2);
document.getElementById("ppDisplay").innerHTML=ppCost;
var ttlCost = fee * gsts;
frm.totalcost.value = ttlCost.toFixed(2);
document.getElementById("grandTotalDisplay").innerHTML=ttlCost;
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
document.getElementById("forfeitTable").style.display="table";
}
// this is just for testing to show which method was actually used
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
// this is in its own if statement so it doesnt popuop while entering data
}
}
// this is the method that should be used below
else if (selectVal == "residualA" ) {
document.getElementById("MCOtable").style.display="none";
var diff = final_orTtl - final_nwTtl;
var displayDiff = diff* -1;
document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
frm.difference.value = diff.toFixed(2);
if ( diff > fee) {
var residual = diff - fee ;
document.getElementById("forfeitInfo").innerHTML = "No additional cost. However,";
frm.lost.value = residual.toFixed(2);
document.getElementById("ppForfeitedDisplay").innerHTML = residual.toFixed(2);
var ttlfForfeited = residual * gsts;
frm.lostTTL.value = ttlfForfeited.toFixed(2);
document.getElementById("totalForfeitedDisplay").innerHTML=ttlfForfeited.toFixed(2);
//document.getElementById("differenceDisplay").innerHTML=0;
frm.difference.value = diff;
document.getElementById("ppDisplay").innerHTML=0;
document.getElementById("grandTotalDisplay").innerHTML=0;
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
document.getElementById("forfeitTable").style.display="table";
}
// this is just for testing to show which method was actually used
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
// this is in its own if statement so it doesnt popuop while entering data
}
}
else {
var remBal = fee - diff ;
var ttlcost = remBal * gsts;
frm.totalcost.value = ttlcost.toFixed(2);
document.getElementById("ppDisplay").innerHTML=remBal.toFixed(2);
document.getElementById("grandTotalDisplay").innerHTML=ttlcost.toFixed(2);
document.getElementById("forfeitTable").style.display="none";
// this is just for testing to show which method was actually used
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
// this is in its own if statement so it doesnt popuop while entering data
}
}
}
else if (selectVal == "residualM" ) {
var diff = final_orTtl - final_nwTtl;
var displayDiff = diff* -1;
document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
frm.difference.value = displayDiff.toFixed(2);
if (diff > fee) {
var mco = diff - fee ;
document.getElementById("MCOInfo").innerHTML=mco.toFixed(2);
frm.MCOamt.value = mco.toFixed(2);
//document.getElementById("differenceDisplay").innerHTML=diff;
//frm.difference.value = diff* -1;
frm.totalcost.value = 0;
document.getElementById("ppDisplay").innerHTML=0;
document.getElementById("grandTotalDisplay").innerHTML=0;
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
document.getElementById("forfeitTable").style.display="none";
document.getElementById("MCOtable").style.display="table";
}
// this is just for testing to show which method was actually used
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
// this is in its own if statement so it doesnt popuop while entering data
}
}
else {
var remBal = fee - diff ;
var ttlcost = remBal * gsts;
frm.totalcost.value = ttlcost.toFixed(2);
document.getElementById("ppDisplay").innerHTML=remBal.toFixed(2);
document.getElementById("grandTotalDisplay").innerHTML=ttlcost.toFixed(2);
document.getElementById("forfeitTable").style.display="none";
document.getElementById("MCOtable").style.display="none";
// this is just for testing to show which method was actually used
if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
// this is in its own if statement so it doesnt popuop while entering data
}
}
}
}
```
```var nwTtl = fltrd_nwTtl / 100;
var final_orTtl = orTtl.toFixed(2)
var orTtl = fltrd_orTtl / 100;
var final_orTtl = orTtl.toFixed(2)
if (final_orTtl > final_nwTtl ) {
}
This part seems to be my problem. the if comparison im doing here doesnt seem to like the '.toFixed(2)' that I did above it for some reason. I changed it to:
var nwTtl = fltrd_nwTtl / 100;
var orTtl = fltrd_orTtl / 100;
if (orTtl > nwTtl ) {
}
Im still testing all the other features but this seems to be working as expected so far. I would still love to know why my use of '.toFixed(2)' causes two numbers that are clearly higher vs lower to evaluate to the opposite. and will accept any answer to that part of this question```

### Getting messages like the user client

```I'm trying to set up an application that will delete all messages from my user.
So far, I'm able to get message since the application started. I've tried using the methods in the documentation but to no luck.
if (cmd == "delete") {
var ch = client.channels.array();
for (i = 0; i < ch.length; i++) {
var c = ch[i];
var xc = client.channels.get(c.id);
if ('messages' in xc) {
var msgs = xc.messages.filter(msg => msg.author.id === client.user.id)
if (!(msgs.array() === undefined) && !(msgs.array().length == 0)) {
for (i = 0; i < msgs.array().length; i++) {
if (msgs.array()[i].type == "DEFAULT") {
let r = await client.channels.get(c.id).messages.get(msgs.array()[i].id).delete();
console.log(r);
}
}
}
}
}
}
I'm trying to get ALL messages from the client user, and delete them.```

### Javascript_ Error in user.js TypeError: Cannot read property 'value' of undefined

```Here is some problems with it.
I would like to ask you how to cope with this problem? Actually I'm new to Javascript.
It looks like this on the console:
Javascript_ Error in user.js TypeError: Cannot read property 'value' of undefined
var s_letters = "qwertyuiopasdfghjklzxcvbnm";
var b_letters = "QWERTYUIOPLKJHGFDSAZXCVBNM";
var digits = "0123456789";
var specials = "!##\$%^&*()_-+=\|/.,:;[]{}";
var is_s = false;
var is_b = false;
var is_d = false;
var is_sp = false;
for (var i = 0; i < password.length; i++) {
if (!is_s && s_letters.indexOf(password[i]) != -1) is_s = true;
else if (!is_b && b_letters.indexOf(password[i]) != -1) is_b = true;
else if (!is_d && digits.indexOf(password[i]) != -1) is_d = true;
else if (!is_sp && specials.indexOf(password[i]) != -1) is_sp = true;
}
var rating = 0;
var text = "";
if (is_s) rating++;
if (is_b) rating++; //
if (is_d) rating++; //
if (is_sp) rating++; //
else if (password.length < 6 && rating >= 3) text = "Good";
else if (password.length >= 8 && rating < 3) text = "Good";
else if (password.length >= 8 && rating >= 3) text = "Excellent";
else if (password.length >= 6 && rating == 1) text = "Bad";
else if (password.length >= 6 && rating > 1 && rating < 4) text = "Good";
else if (password.length >= 6 && rating == 4) text = "Excellent";
console.log(text); // Alert will not work for us here. Console.log allows you to export data to the console for troubleshooting.
return text;
}
var player = GetPlayer(); // Gets the player object.
player.SetVar("SystemDate",newValue); // Set the value of newValue back to Storyline
```
```I've changed your function in a little, try that one (and show us SetValue function):
// #first error - password input, not the form
var expr,
rating = 0,
rules = {
// Lower case
'/[a-z]+/g': false,
// Upper case
'/[A-Z]+/g': false,
// Numbers
'/[0-9]+/g': false,
// Special symbols
'/[^\w\s]/gi': false
};
for (expr in rules) {
// rules[expr] = true; // - can be used to show the proper message of invalidation
rating++;
}
}
return
|| password.length >= 8 && rating >= 3 && "Excellent"
|| password.length < 6 && rating >= 3 && "Good"
|| password.length >= 8 && rating < 3 && "Good"
|| password.length >= 6 && rating > 1 && rating < 4 && "Good"
}
// Gets the player object.
var player = GetPlayer();
// Gets the value for myPassword
// Set the value of newValue back to Storyline
// #second error seems to be in the function SetVar
player.SetVar("SystemDate",newValue);```

```I am creating a game where the user has to try and guess the combination of a code. I want the user to be able to see his previous attempts that he has had. However at the moment I can only get the div box's that they are in to display their most recent code.
Here is my code where the function is.
Game.countAnimals = function(playerGo){
var count = {bulls:0, cows:0};
Game.counter = 1;
for (var i = 0; i < playerGo.length; i++) {
var digPresent = playerGo.indexOf(Game.score[i]);
if (playerGo[i] == Game.score[i]) {count.bulls++;}
else if (digPresent>=0) {count.cows++;}
Game.counter++
if (count.bulls == playerGo.length && Game.counter < 7){
console.log("you have won")}
else if (count.bulls !== playerGo.length && Game.counter < 7) {
console.log("Sorry Homes")}
else if (count.bulls !== playerGo.length && Game.counter > 7 ){console.log ("you lose");}
}
\$(".scoreboard1").html(count.cows);
\$(".scoreboard2").html(count.bulls);```

### Building a dynamic dropdown in javascript

```I have 16 uniquely named dropdowns on a web page. When the page loads, the user has the option on any of the dropdowns to select a value of 0 to 16. 0 is the default on all of them. Now, unless the value is 0, when a user selects a value for one of the dropdowns. I want that value to not be an available option for any of the other dropdowns. This continues until you get to the last dropdown where the only options are the last available number and zero. The issue is, it works fine in Chrome and FireFox, but I can't get it to work correctly in IE. Of course, the majority of the users of the page use IE. The workaround being that all values are always available on all dropdowns, and the javascript checks the values on form post.
I attached the code for the function that does the heavy lifting, this function gets called by an onchange event on each of the dropdowns.
function populatePoints(pointChosen){
for (var k=1; k< 17; k++){
pointValue = document.myform["Dropdown_" + k + "_Points"].value
var theDropDown = document.myform["Dropdown_" + k + "_Points"].options
theDropDown.remove
var x = 0
document.fbpool["Dropdown_" + k + "_Points"].options[x] = new Option(0)
x++
for (var i=1;i<17;i++) {
if (document.myform.Dropdown_1_Points.value != i &&
document.myform.Dropdown_2_Points.value != i &&
document.myform.Dropdown_3_Points.value != i &&
document.myform.Dropdown_4_Points.value != i &&
document.myform.Dropdown_5_Points.value != i &&
document.myform.Dropdown_6_Points.value != i &&
document.myform.Dropdown_7_Points.value != i &&
document.myform.Dropdown_8_Points.value != i &&
document.myform.Dropdown_9_Points.value != i &&
document.myform.Dropdown_10_Points.value != i &&
document.myform.Dropdown_11_Points.value != i &&
document.myform.Dropdown_12_Points.value != i &&
document.myform.Dropdown_13_Points.value != i &&
document.myform.Dropdown_14_Points.value != i &&
document.myform.Dropdown_16_Points.value != i &&
document.myform.Dropdown_15_Points.value != i){
document.myform["Dropdown_" + k + "_Points"].options[x] = new Option(i)
x++}
}
document.myform["Dropdown_" + k + "_Points"].value = pointValue
}
}
```
```If you want to try a different way, this should work for you. As a bonus, you can have as many selects as you need in the page.
Edited: Fixed if conditional with the OP's help.
function matchValue(collection, value) {
// We need this to look for a certain value
// in a collection of values because IE < 9
// doesn't support .indexOf().
for (var i = 0; i < collection.length; i++) {
if (collection[i] == value) {
return true;
}
}
return false;
}
function populatePoints(pointChosen) {
var sels = document.querySelectorAll('select');
// The number of selects returned by the query.
var count = sels.length;
// Value of the select changed.
var pointChosenVal = pointChosen.value;
// Array to keep the current values for all selects.
var chosenValues = [count];
for (var i = 0; i < count; i++) {
// Keeping the current values.
chosenValues[i] = sels[i].value;
}
for (var i = 0; i < count; i++) {
// The current value of this select.
var thisSelVal = sels[i].value;
// Remove all its options.
sels[i].options.length = 0;
// As our selects have an extra option (value = 0),
// and considering that the number of options = number of selects,
// we increase the count by 1.
for (var k = 0; k <= count; k++) {
if (k == 0 ||
(sels[i] == pointChosen && pointChosenVal != 0) ||
((sels[i] != pointChosen || pointChosenVal == 0) && (k == thisSelVal || !matchValue(chosenValues, k)))) {
var opt = document.createElement('option');
opt.value = k;
opt.text = k.toString();
opt.selected = (k == thisSelVal);