I am trying to figure out what is wrong with this following code. The code searches for value in a table and assigns mxCost. The IF..ELSE condition works for all values except 1 and is also reading the table. The IF condition makes the page crash and i am not able to figure out why? Can someone please help me figure out what i am doing wrong here. Any help will be appreciated. Thank you.
$jq("#airTableValues tbody tr").each(function() {
var monthName = months[parseInt(dateSplit[1])-1]+" "+dateSplit[0];
if(monthName==$jq(this).find("td:eq(0)").text().trim())
{
mxCost = $jq(this).find("td:eq(6)").text().trim();
}
});
if (mxCost)
mxCost = (mxCost.substring(1, mxCost.length));
else {
mxCost = 0;
}
var MxStat = document.getElementById("MxStat").value;
MxStat = MxStat/100;
var InflationRate1 = document.getElementById("InflationRate1").value;
InflationRate1 = InflationRate1/100;
var rate3 = document.getElementById("rate3").value;
rate3 = Math.pow(1+rate3/100,1/365)-1;
var mxAdj;
if (MxStat == 1){
mxAdj = mxCost;
} // <------- NOT WORKING!
else if (MxStat != 1) {
var TotalCost = mxCost * 2;
mxAdj = (TotalCost * MxStat) - mxCost;
}
var ReturnVal = (mxAdj * InflationRate1) + mxAdj;
var ReturnCalc = ReturnVal * 1000000;
var ReturnPV = Math.floor(ReturnCalc/Math.pow(1+rate3,periods));
document.getElementById("ReturnCondition").value =
(ReturnPV/1000000).toFixed(2);
ReturnCondition = ReturnVal.toFixed(2);
This worked for me
if (MxStat != 1){
var mxAdj = (TotalCost * MxStat) - mxCost;
var ReturnVal = (mxAdj * InflationRate1) + mxAdj;
aReturnCondition = ReturnVal.toFixed(2);
}
if (MxStat == 1){
var mxAdj = mxCost;
var ReturnVal = (mxAdj * InflationRate1) + mxAdj;
aReturnCondition = parseFloat(ReturnVal).toFixed(2);}
Related
var site = document.getElementById('display').innerHTML =
document.getElementById("site").value;
var link = document.getElementById('displayy').innerHTML =
document.getElementById("link").value;
var t = true;
var both = [site,link];
while (t == true){
var s = 0;
var y = 0;
var check = localStorage.getItem(y);
if (check === null){
localStorage.setItem(s , both);
alert(y);
break;
} else {
var s = s + 1;
var y = y + 1;
localStorage.setItem(s , both);
}
}
Javascript should check if 0 = emty if so then add it to localstorarge if no check 1 check 2 check 3 etc. I get the data from html.
I am creating a program which will sort people's names into different tables(table1, table2, table3, table4) which are represented by arrays and it will add any preferences that people would like to sit by into the same table as them. I have a function, check(), which makes sure there are no duplicate names and that the tables' length does not exceed 6. The problem is the check() function moves the preferences out of the table they need to be in. Is there any way to keep this from happening? Thank you.
JavaScript:
$(document).ready(function () {
var table1 = [];
var table2 = [];
var table3 = [];
var table4 = [];
var names = [];
var pref = [];
function seat() {
for (var i = 0; i < names.length; i++) {
if (pref != "") {
if (pref == names[i]) {
var who = names[i];
function prandomize(min, max) {
var pr = Math.floor(Math.random() * (max - min + 1)) + min;
if (pr == 1) {
table1.push(names[i]);
table1.push(who);
} else if (pr == 2) {
table2.push(names[i]);
table2.push(who);
} else if (pr == 3) {
table3.push(names[i]);
table3.push(who);
} else if (pr == 4) {
table4.push(names[i]);
table4.push(who);
} else {
return "Error: Contact Source Code Author!!";
}
}
}
}
function randomize(min, max) {
var r = Math.floor(Math.random() * (max - min + 1)) + min;
if (r == 1) {
table1.push(names[i]);
} else if (r == 2) {
table2.push(names[i]);
} else if (r == 3) {
table3.push(names[i]);
} else if (r == 4) {
table4.push(names[i]);
} else {
return "Error: Contact Source Code Author!!";
}
}
randomize(1, 4);
};
console.log(table1);
console.log(table2);
console.log(table3);
console.log(table4);
console.log("first call");
};
var htable1 = document.getElementById('t1');
var htable11 = document.getElementById('t11');
var htable111 = document.getElementById('t111');
var htable1111 = document.getElementById('t1111');
var htable11111 = document.getElementById('t11111');
var htable111111 = document.getElementById('t111111');
var htable2 = document.getElementById('t2');
var htable22 = document.getElementById('t22');
var htable222 = document.getElementById('t222');
var htable2222 = document.getElementById('t2222');
var htable22222 = document.getElementById('t22222');
var htable222222 = document.getElementById('t222222');
var htable3 = document.getElementById('t3');
var htable33 = document.getElementById('t33');
var htable333 = document.getElementById('t333');
var htable3333 = document.getElementById('t3333');
var htable33333 = document.getElementById('t33333');
var htable333333 = document.getElementById('t333333');
var htable4 = document.getElementById('t4');
var htable44 = document.getElementById('t44');
var htable444 = document.getElementById('t444');
var htable4444 = document.getElementById('t4444');
var htable44444 = document.getElementById('t44444');
var htable444444 = document.getElementById('t444444');
function check() {
var stable1 = table1.slice().sort();
for (var i = 0; i < table1.length - 1; i++) {
if (stable1[i + 1] == stable1[i]) {
table1.splice(i, 1);
console.log("removed");
}
}
if (table1.length > 6) {
while (table1.length > 6) {
var lastvalue = table1.pop();
table2.push(lastvalue);
console.log("moved to table2");
}
}
if (table2.length > 6) {
while (table2.length > 6) {
var lastvalue2 = table2.pop();
table3.push(lastvalue2);
console.log("moved to table3");
}
}
if (table3.length > 6) {
while (table3.length > 6) {
var lastvalue3 = table3.pop();
table4.push(lastvalue3);
console.log("moved to table4");
}
}
if (table4.length > 6) {
while (table4.length > 6) {
var lastvalue4 = table4.pop();
table1.push(lastvalue4);
console.log("moved to table1");
}
}
}
function changeHTML() {
htable1.innerHTML = table1[0];
htable11.innerHTML = table1[1];
htable111.innerHTML = table1[2];
htable1111.innerHTML = table1[3];
htable11111.innerHTML = table1[4];
htable111111.innerHTML = table1[5];
htable2.innerHTML = table2[0];
htable22.innerHTML = table2[1];
htable222.innerHTML = table2[2];
htable2222.innerHTML = table2[3];
htable22222.innerHTML = table2[4];
htable222222.innerHTML = table2[5];
htable3.innerHTML = table3[0];
htable33.innerHTML = table3[1];
htable333.innerHTML = table3[2];
htable3333.innerHTML = table3[3];
htable33333.innerHTML = table3[4];
htable333333.innerHTML = table3[5];
htable4.innerHTML = table4[0];
htable44.innerHTML = table4[1];
htable444.innerHTML = table4[2];
htable4444.innerHTML = table4[3];
htable44444.innerHTML = table4[4];
htable444444.innerHTML = table4[5];
}
function namesdefine() {
names.push(document.getElementById('nameone').value);
names.push(document.getElementById('nametwo').value);
names.push(document.getElementById('namethree').value);
names.push(document.getElementById('namefour').value);
names.push(document.getElementById('namefive').value);
names.push(document.getElementById('namesix').value);
names.push(document.getElementById('nameseven').value);
names.push(document.getElementById('nameeight').value);
names.push(document.getElementById('namenine').value);
names.push(document.getElementById('nameten').value);
names.push(document.getElementById('nameeleven').value);
names.push(document.getElementById('nametwelve').value);
names.push(document.getElementById('namethirteen').value);
names.push(document.getElementById('namefourteen').value);
names.push(document.getElementById('namefifthteen').value);
names.push(document.getElementById('namesixteen').value);
names.push(document.getElementById('nameseventeen').value);
names.push(document.getElementById('nameeighteen').value);
names.push(document.getElementById('namenineteen').value);
names.push(document.getElementById('nametwenty').value);
names.push(document.getElementById('nametwentyone').value);
names.push(document.getElementById('nametwentytwo').value);
names.push(document.getElementById('nametwentythree').value);
names.push(document.getElementById('nametwentyfour').value);
console.log(names);
var testvar = document.getElementById('nameone').value;
console.log(testvar);
console.log("Look here please");
}
function prefsdefine() {
pref.push(document.getElementById('prefone').value);
pref.push(document.getElementById('preftwo').value);
pref.push(document.getElementById('prefthree').value);
pref.push(document.getElementById('preffour').value);
pref.push(document.getElementById('preffive').value);
pref.push(document.getElementById('prefsix').value);
pref.push(document.getElementById('prefseven').value);
pref.push(document.getElementById('prefeight').value);
pref.push(document.getElementById('prefnine').value);
pref.push(document.getElementById('preften').value);
pref.push(document.getElementById('prefeleven').value);
pref.push(document.getElementById('preftwelve').value);
pref.push(document.getElementById('prefthirteen').value);
pref.push(document.getElementById('preffourteen').value);
pref.push(document.getElementById('preffifthteen').value);
pref.push(document.getElementById('prefsixteen').value);
pref.push(document.getElementById('prefseventeen').value);
pref.push(document.getElementById('prefeightteen').value);
pref.push(document.getElementById('prefnineteen').value);
pref.push(document.getElementById('preftwenty').value);
pref.push(document.getElementById('preftwentyone').value);
pref.push(document.getElementById('preftwentytwo').value);
pref.push(document.getElementById('preftwentythree').value);
pref.push(document.getElementById('preftwentyfour').value);
}
document.getElementById('sbm').addEventListener('click', function (e) {
e.preventDefault();
namesdefine();
prefsdefine();
seat();
check();
check();
check();
changeHTML();
});
console.log(table1);
console.log(table2);
console.log(table3);
console.log(table4);
console.log("second call");
console.log(pref);
});
I would suggest that you first focus on the data structures that you are using before you try to go too much further. The preferences handling is trickier than it seems at first because you could have person1 who wants to sit with person2, but person2 wants to sit beside person6. This creates a chain of preferences that is not trivial to handle.
Before you try to tackle that, I would suggest you try creating a single data structure (rather than four tables). This data structure might be an array of size 24 holding objects that looks something like this:
{ name: 'xxx', preference: 'yyy', table: n }
If you are not comfortable with objects, or arrays of objects, I would suggest that you focus your efforts on learning them since they are absolutely essential in JavaScript. Arrays are handy but, in practice, they tend to be for very simple lists or as containers to hold objects.
I could provide you code that will do what you want but I don't think that will help you in your learning curve. Take a stab at applying a different data structure and if you get stuck there, maybe post a new question on SO. There are plenty of people that want to help but you will need to do your homework to get assistance along the way.
Im trying to make an online Role Playing Game, but my code is not working. It asks the user what they want their character to be named, and what race they want to be. It would then randomly choose some stats for them, and -- depending upon their race -- add and subtract from their stats.
var nameAndRaceFunction = function(){
var namePrompt = prompt("What shall you name your character?");
var racePrompt = prompt("What race shall your character be? Please spell it correctly, with no capitals.");
var race = racePrompt.toLowerCase();
var totalSentence = namePrompt + " the " + race;
document.getElementById("nameAndRace").innerHTML = totalSentence;
}
var str = Math.floor((Math.random() * 10) + 1);
var int = Math.floor((Math.random() * 10) + 1);
var hlth = Math.floor((Math.random() * 10) + 1);
var dext = Math.floor((Math.random() * 10) + 1);
var getStatFunction = function(){
if(racePrompt === "elf"){
elfStats();
}else if(racePrompt === "dwarf"){
dwarfStats();
}else if(racePrompt === "man"){
manStats();
}else{
}
}
var elfStats = function(){
var elfStr = str;
var elfInt = int + 1;
var elfHlth = (hlth - 1)*10;
var elfDext = dext + 1;
document.getElementById("strength").innerHTML = elfStr;
document.getElementById("intelligence").innerHTML = elfInt;
document.getElementById("health").innerHTML = elfHlth;
document.getElementById("dexterity").innerHTML = elfDext;
}
var manStats = function(){
var manStr = str + 2;
var manInt = int;
var manHlth = (hlth - 1) * 10;
var manDext = dext;
document.getElementById("strength").innerHTML = manStr;
document.getElementById("intelligence").innerHTML = manInt;
document.getElementById("health").innerHTML = manHlth;
document.getElementById("dexterity").innerHTML = manDext;
}
var dwarfStats = function(){
var dwarfStr = str + 1;
var dwarfInt = int;
var dwarfHlth = (hlth + 1) * 10;
var dwarfDext = dext - 1;
document.getElementById("strength").innerHTML = dwarfStr;
document.getElementById("intelligence").innerHTML = dwarfInt;
document.getElementById("health").innerHTML = dwarfHlth;
document.getElementById("dexterity").innerHTML = dwarfDext;
}
racePrompt is defined inside the nameAndRaceFunction() function scope. It is not accessible outside of it. Further: you lower case it, so later I will check only for race not racePrompt.
One solution would be to make race global like str, int, hlth, dext
var nameAndRaceFunction = function() {
namePrompt = prompt("What shall you name your character?");
var racePrompt = prompt("What race shall your character be? Please spell it correctly, with no capitals.");
race = racePrompt.toLowerCase();
var totalSentence = namePrompt + " the " + race;
document.getElementById("nameAndRace").innerHTML = totalSentence;
getStatFunction()
}
var namePrompt, race;
var str = Math.floor((Math.random() * 10) + 1);
var int = Math.floor((Math.random() * 10) + 1);
var hlth = Math.floor((Math.random() * 10) + 1);
var dext = Math.floor((Math.random() * 10) + 1);
var getStatFunction = function() {
if (race === "elf") {
elfStats();
} else if (race === "dwarf") {
dwarfStats();
} else if (race === "man") {
manStats();
} else {
}
}
getStatFunction should be called with the race as argument or you should define the variable racePrompt outsite the function
Also, if you mean to make a player, a good idea should be have it as an object and use the nameAndRaceFunction like a constructor
I've basic knowledge of JS. I'm trying to make the two functions below to work together but I cannot find a solution. It's either one or the other that work but not the two together.
<script>
function random_string(size){
var str = "";
for (var i = 0; i < size; i++){
str += random_character();
}
return str;
}
function random_character() {
var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
return chars.substr( Math.floor(Math.random() * 62), 1);
}
window.onload = function () {
var partnerurl = document.getElementById('field1');
uniqueid = document.getElementById('field2');
uniqueid.value = random_string(15);
partnerurl.value = "www.website.com"+uniqueid.value
};
</script>
<script>
var defaultHiddenFieldNameValue = "";
function getQueryStringParamValue(strQStrParam) {
var strURL = document.location.href;
var strQStrParamValue = "";
if (strURL.indexOf('?') != -1)
{
strQStrParamValue = strURL.substr(strURL.indexOf('?') + 1);
if (strQStrParamValue.indexOf(strQStrParam) != -1)
{
strQStrParamValue = strQStrParamValue.substr(strQStrParamValue.indexOf(strQStrParam));
strQStrParamValue = strQStrParamValue.substr(strQStrParamValue.indexOf('=') + 1);
if (strQStrParamValue.indexOf('&') != -1)
strQStrParamValue = strQStrParamValue.substr(0, strQStrParamValue.indexOf('&'));
return strQStrParamValue;
}else{
strQStrParamValue = defaultHiddenFieldNameValue;
return strQStrParamValue;
}
}else{
strQStrParamValue = defaultHiddenFieldNameValue;
return strQStrParamValue;
}
}
// Form name goes here
var form = "formname";
function setCampaign(){
var elqForm = document.forms[form];
//repeat for each field to populate
elqForm.elements['src'].value = getQueryStringParamValue('src');
}
window.onload = setCampaign;
</script>
Many thanks in advance for your help!
Cheers,
FX
In your code sample here you are missing a semicolon on the last line of the onload function.
I also don't see you declaring uniqueid.
Your random string function works fine, its just getting the window.onload to fire that I had problems with.
Here is a demostration of your random string working fine: http://jsfiddle.net/Jimmery/tv87X/
I would suggest loading jQuery with this line in your HTML:
<script src="ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
and then use this:
$(document).ready(function(){
var partnerurl = $('field1');
var uniqueid = $('field2');
uniqueid.val(random_string(15));
partnerurl.val("www.website.com/"+uniqueid.val());
};
As an extension question that Felix Kling answered so brilliantly I now need to do a two part check on the data table deployed.
Can anyone tell me how to add to the code below to allow me not only to copy the values of the pcode fields into an array but to check if there is a check in the checkbox with a corresponding row number i.e. route2 is check then add to the array but if route3 is not checked then exclude it.
function GetRoute()
{
var from = document.getElementById('inpAddr').value;
var locations = [from];
for(var i = 2; i <= 400; i++) {
var element = document.getElementById('pcode' + i);
if(element === null) { break; }
locations.push(element.innerHTML);
}
var options = new VERouteOptions();
options.DrawRoute = true;
options.RouteColor = new VEColor(63,160,222,1);
options.RouteWeight = 3;
options.RouteCallback = onGotRoute;
options.SetBestMapView = true;
options.DistanceUnit = VERouteDistanceUnit.Mile;
options.ShowDisambiguation = true;
map.GetDirections(locations,options);
}
Thanks in advance!
Justin
for( var i = 2 ; (element = document.getElementById('pcode' + i)) && i <= 400; i++ )
{
if( document.getElementById('route' + i).checked )
{
locations.push( element.innerHTML );
}
}
function GetRoute() {
var from = document.getElementById('inpAddr').value;
var locations = [from];
// My Modification Starts Here....
var maxLoopValue = 400;
var pcode, currentRoute, nextRoute;
for(var i = 2; i <= maxLoopValue; i++) {
pcode = document.getElementById('pcode' + i);
currentRoute = document.getElementById('route' + i);
// Make sure the currentRoute is 'checked'
if (currentRoute.checked) {
// Make sure there is a 'next' route before trying to check it
if (i < maxLoopValue) {
nextRoute = document.getElementById('route' + (i+1));
// Make sure the 'next' route is 'checked'
if (nextRoute.checked) {
locations.push(element.innerHTML);
}
} else {
// This is the last route, since we know it's checked, include it
locations.push(element.innerHTML);
}
}
}
// My Modification Ends Here....
var options = new VERouteOptions();
options.DrawRoute = true;
options.RouteColor = new VEColor(63,160,222,1);
options.RouteWeight = 3;
options.RouteCallback = onGotRoute;
options.SetBestMapView = true;
options.DistanceUnit = VERouteDistanceUnit.Mile;
options.ShowDisambiguation = true;
map.GetDirections(locations,options);
}