JavaScript Output Returning NaN - javascript

I am using App Lab on Code.org, which utilizes JavaScript commands; however, they have their own UI controls. Therefore, the code will contain commands such as onEvent() and setText(), etc. etc. These are all acceptable. I am attempting to make a code for the Spherical Law of Cosines, but my output for the number is printing NaN. What does this mean and how can I fix it?
Part of my code is below:
onEvent("outputgoScrn","click",function(){
setScreen("outputScrn");
setText("output","The distance between these two locations is " + ((Math.acos((Math.cos(a)*(180/Math.PI))*(Math.cos(b)*(180/Math.PI)) + (Math.sin(a)*(180/Math.PI))*(Math.sin(b)*(180/Math.PI)) + (Math.cos(N)*(180/Math.PI))*(180/Math.PI)))) + " miles along the Great Circle.");
});
onEvent("lat1", "change", function() {
var choice = getProperty("lat1","value");
if(choice=="N") {
a = 90 - ((getNumber("lat1deg")+(getNumber("lat1min")/60)));
}
else if(choice=="S") {
a = 90 + (getNumber("lat1deg")+(getNumber("lat1min")/60));
}
});
onEvent("lat2", "change", function() {
var choice = getProperty("lat2","value");
if(choice=="N") {
b = 90 - ((getNumber("lat1deg")+(getNumber("lat1min")/60)));
}
else if(choice=="S") {
b = 90 + (getNumber("lat2deg")+(getNumber("lat2min")/60));
}
});
onEvent("lon1", "change", function() {
var choice = getProperty("lon1","value");
onEvent("lon2","change",function() {
var choice2 = getProperty("lon2","value");
if(choice=="E" && choice2=="E") {
N = (getText(("lon1deg")+(getText("lon1min")/60))) - (getText(("lon2deg")+(getText("lon2min")/60)));
}
else if(choice=="W" && choice2=="W") {
N = getText(("lon1deg")+(getText("lon1min")/60)) - getText(("lon2deg")+(getText("lon2min")/60));
}
else if(choice=="W"&&choice2=="E") {
N = getText(("lon1deg")+(getText("lon1min")/60)) + getText(("lon2deg")+(getText("lon2min")/60));
}
else if(choice=="E"&&choice2=="W") {
N = getText(("lon1deg")+(getText("lon1min")/60)) + getText(("lon2deg")+(getText("lon2min")/60));
}
});
});

NaN stands for "Not a Number". It looks like you are trying to do arithmetic operations with strings. You need to parse your string to number
var text = '42px';
var integer = parseInt(text, 10);
// returns 42
For more parse functions, you can check below link
https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/

Related

Why isn't my program assigning variables to the object, and then calculating from it?

This assignment is to take a pizza order and build one. Once the pizza is assembled, it should calculate the total cost of the pizza and spit out the total answer. I've played around on JsFiddle.net for two days now and can't figure out why it won't get past the interrogate function and move on to the calculate. I've entered in a couple alert statements here and there and still can't figure out what's going on. My best guess is either that 'myPizza' properties are not being assigned or that they are not being shared by the other functions. I know the second possibility could not be true because 'myPizza' is a global object with global properties.
recycle = true;
var ingrediants = ['pepperoni ', 'sausage ', 'avocado ', 'chicken ', 'mushrooms ', 'anchovies ', 'bacon ', 'pineapple ', 'beeswax '];
var toppings=[];
var i = -1;
var myPizza= {
size:"",
toppings:[],
stuffCrust:false,
sodaSide: false
};
var greeting = function () {
wantPizza = prompt("Hello! Welcome to Sean's Pizza Shack! Would you like to order a pizza?(Yes or No?)").toLowerCase();
if (wantPizza === 'yes') {
return true;
} else {
alert("Okay then, Get out of my pizza shop because I have customers waiting");
return false;
}
};
var interrogate = function () {
myPizza.size = prompt("Okay sir/Ma'am, Would you like a Small, Medium, Large, or extra Large?(XL)").toLowerCase();
myPizza.toppings = prompt("Alright! So what would you like on your " + myPizza.size + " pizza?" + " We have " + ingrediants).toLowerCase();
do {
i = i + 1;
myPizza.toppings+= toppings[i] =prompt(ingrediants + " Please type one ingrediant at a time to add to your pizza! or you may enter 'ALL' to add all toppings or press OK without entry to stop adding toppings").toLowerCase();
} while (toppings[i] !== "");
//LOOP TO DECIDE IF NEED TO PUSH ALL INGREDIENTS
for (k = 0; k <toppings.length; k++) {
if (toppings[k] === 'all') {
toppings = [];
toppings.push(ingrediants);
} else {
toppings.length -= 1; // IF NOT ALL INGREDIENTS, SUBTRACT '' FROM ADD INGREDIENTS //LOOP
}
}
alert("So you would like " + myPizza.toppings + " on your pizza!");
alert("Okay so i have a " + myPizza.size + " pizza, with " + myPizza.toppings + "!");
myPizza.stuffCrust = prompt("Do you want your pizza to have extra delicious stuffed cheesy crust?($4.00)").toLowerCase();
if(myPizza.stuffCrust==='yes') {
myPizza.stuffCrust=true;
}
myPizza.sodaSide = prompt("Would you like a 2 Liter soda with that for an extra $2.00?");
if(myPizza.sodaSide=== yes) {
myPizza.sodaSide=true;
}
alert(myPizza.sodaSide);
alert(myPizza.toppings);
alert(myPizza.stuffCrust);
alert(myPizza.toppings.length);
};
var up= {
total:0,
Sm:9.00,
Med:12.00,
Lrg: 15.00,
XL: 18.00,
Top: myPizza.toppings.length*1.00,
Stuff:4.00,
Soda: 2.00,
add: function(input) {
total+=input;
}
};
var calculate= function() {
switch(myPizza.size) {
case 'small': up.add(up.Sm);break;
case 'medium': up.add(up.Med);break;
case 'large': up.add(up.Lrg);break;
case 'XL': up.add(up.XL);break;
}
if(myPizza.toppings.length>0) {
up.add(up.Top);
} if (myPizza.stuffCrust) {
up.add(up.Stuff);
} if (myPizza.sodaSide) {
up.add(up.Soda);
}
alert("Okay, looks like your order comes to "+ up.total);
};
var main= function () {
if (greeting() === true) {
interrogate();
calculate();
}
};
main();
in the statement:
if(myPizza.sodaSide=== yes) {
myPizza.sodaSide=true;
}
you need to have yes in quotes:
if(myPizza.sodaSide=== 'yes') {
myPizza.sodaSide=true;
}

reading and writing to taffydb

I can't seem to get data from my taffyDB.
It has to be bad syntax but I don't know where.
My Javascript looks like this:
// init db
var procTech = TAFFY();
//..... other code in the middle
procTech().remove();
var x = 0;
$(".sxRow select[id^='KRCtech_']").each(function() {
var techName = $(this).val();
x++;
var xStr = x.toString();
clog(xStr + " " + techName);
procTech.insert({"count":xStr,"tech":techName });
});
var ret = eval(procTech().select("count","tech"));
clog(ret.length);
for (j = 0; j <= ret.length - 1; j++) {
clog("read back: " + [j][0] + "," + [j][1]);
}
// wrapper for console.log
function clog(s) {
window.console && console.log(s);
return;
}
console says:
1 tonya
2 shawn
2
read back: 0,undefined
read back: 1,undefined
so I know that
my initial values are good; i.e. they have value
Taffy sees 2 records, which is correct
It's just when I try and retrieve them, they are garbage.
What am I doing wrong?
procTech.insert({"count":1,"tech":'techName' });
procTech.insert({"count":2,"tech":'techName1' });
procTech.insert({"count":3,"tech":'techName2' });
procTech.insert({"count":3,"tech":'techName2' });
procTech.insert({"count":3,"tech":'techName2' });
procTech.insert({"count":4,"tech":'techName3' });
var query = procTech.select("count","tech"); // 3 rows
for ( var x=0; x<query.length-1; x++ ) {
console.log(query[x][0], query[x][1]);
}

display the recursion line by line

I am trying to make a function in javascript that would expand/split a string with dashes and show the process ( line by line ) using recursion.
for example, the string "anna" would become:
expand("anna") = expand("an")+"---"+expand("na") ->
"a"+"---"+"n"+"---"+"n"+"---"+"a"
and the desired output would be:
anna
an---na
a---n---n---a
I have achieved doing the following so far (I know it might not be the solution I am looking):
expand("anna") = an+"---"+expand("na")
= an+"---"+n+"---"+expand("a");
= an+"---"+n+"---+"a"
the output I am getting is:
an---n---a
I can't seem to concatenate the head though to do the first example.
My javascript function of expand is as follows:
function expand(word) {
if (word.length<=1) {
return word;
} else {
mid = word.length/2;
return word.substr(0,mid) + " " + expand(word.substr(mid,word.length));
}
}
document.write(expand("anna"));
I would need some tips to do this, otherwise (if it's the wrong stackexchange forum), please guide me where to post it.
this is my crazy attempt
var Word = function(str) {
this.isSplitable = function() {
return str.length > 1;
}
this.split = function() {
var p = Math.floor(str.length / 2);
return [
new Word(str.substr(0,p)),
new Word(str.substr(p,p+1))
];
}
this.toString = function() {
return str;
}
}
var expand = function(words) {
var nwords = [];
var do_recur = false;
words.forEach(function(word){
if(word.isSplitable()) {
var splitted = word.split();
nwords.push(splitted[0]);
nwords.push(splitted[1]);
do_recur = true;
}else{
nwords.push(word);
}
});
var result = [];
nwords.forEach(function(word){
result.push( word.toString() );
});
var result = result.join("--") + "<br/>";
if(do_recur) {
return result + expand(nwords);
}else{
return "";
}
}
document.write( expand([new Word("anna")]) );
This is what you need
expand = function(word) {
return [].map.call(word, function(x) {return x+'---'}).join('')
};
The joy of functional programming.
And with added code to deal with last character:
function expand(word) {
return [].map.call(word, function(x, idx) {
if (idx < word.length - 1)
return x+'---';
else return x
}).join('')
}
As I said that it is impossible to display the "process" steps of recursion while using recursion, here is a workaround that will output your desired steps:
var levels = [];
function expand(word, level) {
if (typeof level === 'undefined') {
level = 0;
}
if (!levels[level]) {
levels[level] = [];
}
levels[level].push(word);
if (word.length <= 1) {
return word;
} else {
var mid = Math.ceil(word.length/2);
return expand(word.substr(0, mid), level+1) + '---' + expand(word.substr(mid), level+1);
}
}
expand('anna');
for (var i = 0; i < levels.length; i++) {
console.log(levels[i].join('---'));
}
to see all steps the best that I whold do is:
function expand(word) {
if (word.length<=1) {
return word;
} else {
var mid = word.length/2;
var str1 = word.substr(0,mid);
var str2 = word.substr(mid,word.length);
document.write(str1 + "---" + str2 + "<br></br>");
return expand(str1) + "---" + expand(str2);
}
}
document.write(expand("anna"));
You have to return the two parts of the string:
function expand(word) {
output="";
if (word.length<=1) {
output+=word;
return output;
} else
{
var mid = word.length/2;
output+=word.substr(0,mid)+"---"+word.substr(mid)+" \n";//this line will show the steps.
output+=expand(word.substr(0,mid))+"---"+expand(word.substr(mid,word.length-1))+" \n";
return output;
}
}
console.log(expand("anna"));
Edit:
I added the output var and in every loop I concatenate the new output to it.
It should do the trick.
Hope the problem is in your first part. According to your algorithm, you are splitting your string anna in to two parts,
an & na
so you need to expand both parts until the part length is less than or equal to one. so your required function is the below one.
function expand(word) {
if (word.length<=1) {
return word;
} else {
mid = word.length/2;
return expand(word.substr(0,mid)) + " --- " + expand(word.substr(mid,word.length));
}
}
document.write(expand("anna"));

datatables order column for uk date and time

I am trying to sort a column in a table via the DataTables pluing that has a UK date and time like this: 21/09/2013 11:15
Using the code from Ronan Guilloux:
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"uk_date-pre": function ( a ) {
if ($.trim(a) != '') {
var frDatea = $.trim(a).split(' ');
var frTimea = frDatea[1].split(':');
var frDatea2 = frDatea[0].split('/');
var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
} else {
var x = 10000000000000; // = l'an 1000 ...
}
return x;
},
"uk_date-asc": function ( a, b ) {
return a - b;
},
"uk_date-desc": function ( a, b ) {
return b - a;
}
} );
and also ive added this code to detect it automatically so i don't have to set which column it is for:
jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData )
{
if (sData !== null && sData.match(/(0[1-9]|[12]\d|3[0-2])\/(0[1-9]|1[0-2])\/\d{4} (0[1-9]|1\d|2[0-3]):(0[1-9]|[1-5]\d)$/))
{
//console.log(sData);
return 'uk_date';
}
return null;
}
);
The problem i have is that although i can see the regex is matching the string it is not then calling the 'uk_date-pre', 'uk_date-asc' or 'uk_date-desc' can anyone explain why it is not working?
After playing with it for some time i had to abandon the regEx and i simply added this to the set up:
aoColumnDefs: [{ "sType": "datetime-uk", "aTargets": [whichCol] }]
I then set the whichCol var to either null or a column number if it was on the page that needed this UK sorting.
For anyone stumbling on this.
The code from Ronan Guilloux will work as expected if you change :
var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
to:
var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1]) * 1;
The reason being that we are dealing with "21/09/2013 11:15" therefore
var frTimea = frDatea[1].split(':');
will only populate frTimea[0] and frTimea[1]...

JavaScript isCurrency() failing

hey guys should be a easy one...I have some javascript that is turning my input values into currency values. Problem is it will fail if I try to type in .5 heres is my code:
function handleCurrency(formName,fieldName)
{
var enteredValue = document.forms[formName].elements[fieldName].value;
if ( enteredValue.isCurrency() )
{
alert("This is currency " + enteredValue )
// Put the nicely formatted back into the text box.
document.forms[formName].elements[fieldName].value = enteredValue.toCurrency();
}
}
jsp:
<td><input type="text" name="replacementCost" onchange="handleCurrency('NsnAdd','replacementCost')" value="<ctl:currencyFormat currency='${form.replacementCost}'/>" onkeypress="javascript:return noenter();" <c:if test="${!lock.locked}">disabled="disabled"</c:if> /></td>
How can I make it so that .5 is allowable also to be formatted?
custom javascript:
var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$");
var patternWithoutCommas = new RegExp("^\\s*\\$?-?\\d+(\\.\\d{1,2})?\\s*$");
function stringIsCurrency()
{
if (patternWithoutCommas.test(this))
{
return true;
}
else if (patternWithCommas.test(this))
{
return true;
}
return false;
}
function stringToCurrency()
{
if (this == '') return this;
var str = this.replace(/[$,]+/g,'');
sign = (str == (str = Math.abs(str)));
str = Math.floor(str*100+0.50000000001);
cents = str%100;
str = Math.floor(str/100).toString();
if (cents<10) cents = "0" + cents;
for (var i = 0; i < Math.floor((str.length-(1+i))/3); i++)
{
str = str.substring(0,str.length-(4*i+3))+','+
str.substring(str.length-(4*i+3));
}
str = '$' + ((sign)?'':'-') + str + '.' + cents;
return str;
}
String.prototype.isCurrency = stringIsCurrency;
String.prototype.toCurrency = stringToCurrency;
basically it needs to allow .5 and not just 0.5
this needs to be updated:
var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$");
You have not shown your code for isCurrency.
Here's how I would do it:
function isCurrency( val )
{
return /^\$?(?:\d[\d,]*)?(?:.\d\d?)?$/.test( val );
}
See it here in action: http://regexr.com?3103a
Now that you have provided your code, here's my proposed solution.
While there are many things I would have done differently,
in order to keep the spirit of your code, just change this:
var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$");
var patternWithoutCommas = new RegExp("^\\s*\\$?-?\\d+(\\.\\d{1,2})?\\s*$");
to this:
var patternWithCommas = /^\s*\$?-?((\d{1,3}){1}(,\d{3})?)?(\.\d{1,2})?\s*$/;
var patternWithoutCommas = /^\s*\$?-?(\d+)?(\.\d{1,2})?\s*$/;
which would make the dollar amount optional.

Categories

Resources