I am making a simple cost estimator. It takes 3 imputed values X,Y,Z and displays a price which is a simple calculation of a value by the z axis (the third of 3 collected values).
You can view the estimator here: http://codepen.io/FredHair/pen/FgJAd
What I would like to add is a checkbox that when clicked would check that the smallest value is stored as the Z axis. So if the user imputed 3 values and the X or Y value was the smallest then that value would be swapped with with the z axis value.
How would I go about writing a function for this?
Any help would be greatly appreciated even in pseudo code, which I could then write myself.
I have made a checkbox:
<input type="checkbox" id="zAxis">Z Axis Optimization<br>
This is my function for the Estimator:
//Calc with Switch//
function calculator(){
var x = Number(document.getElementById("x").value);
var y = Number(document.getElementById("y").value);
var z = Number(document.getElementById("z").value);
var p = Number(3);
var result;
var calc = document.getElementById("choice").value
switch(calc){
case"1" : result = z * p;
break;
case"2" : result = (z * p) + 50;
break;
case"3" : result = (z * p) + 30;
break;
}
//Display Result//
document.getElementById("result").innerHTML = " = £ " + result;
I know I will need to write a function like:
IF (x<=y && <=z) then do something
OR (y<=y && <=x) then do something
But any help will be greatly appreciated.
Here is Codepen again: http://codepen.io/FredHair/pen/FgJAd
Thanks in advance.
Related
I am writing a Javascript function where strings with apostrophe ( ' ) are not being displayed. Is there a way I can go around this? The below function assumes x is a string.
function addItem(x, y) //adds item on screen and adds Total
{
var newRow = "<tr><td>"+x+"</td><td>€"+y.toFixed(2)+"</td><td><input type=\"button\" onclick=\"subtract("+y+")\" value = \"X\"></td></tr>"
$('#order').append(newRow);
document.getElementById("currentorder").value += newRow;
//Adds Total Value
var total = document.getElementById('price').innerHTML;
total = parseFloat(total);
var z = +y + +total;
document.getElementById('price').innerHTML=z.toFixed(2);
event.preventDefault();
}
Confusing Addition Operators
The following line looks quite a bit off :
var z = +y + +total;
If you just want to add y and total, just simplify it as :
var z = y + total;
Consider Possible Parsing Errors
Another idea would be to consider stripping out any non float related characters (i.e. non decimals and digits) within your string prior to calling your parseFloat() function :
var total = parseFloat(document.getElementById('price').innerHTML.replace(/[^\d\.]/g,''));
I have to spreadsheets. I want the program to look at Row A on spreadsheet Ind and see if it is a 1 or 0. if it is a one on the active sheet "return" I want it to grab the date from Row D in spreadsheet "Ind" and post it onto Spreadhseet "return". I can't figure this out and I have it working on VBA in excel.
Any help would be greatly appreciated.
function myFunction() {
X = 5;
Y = 2;
Z = 1;
Count = 4560;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = ss.getSheetByName("Ind");
var target_sheet = ss.getSheetByName("Returns");
while (Z < Count){
if (source_sheet.getRange("A" & X) = 1) {
var buydate = source_sheeet.getRange("D" & X).getValues()
target_sheet.getRange("A" & Y) = buydate
target_sheet.getRange("B" & Y) = "Buy"
Y = Y + 1
} else if (source_sheeet.Range("C" & X) = 2) {
var selldate = source_sheeet.Range("D" & X).getvalues()
target_sheet.getRange("A" & Y) = selldate
target_sheet.getRange("B" & Y) = "Sell"
Y = Y + 1
}
X = X + 1
Z = Z + 1
}}
This line:
if (source_sheet.getRange("A" & X) = 1) {
Is using an ampersand, and it should be a plus sign. To concatenate strings in JavaScript, use a plus sign.
Also, source_sheet.getRange() will return a range, not a value, so it's never going to equal 1. You would need to use something like the following:
if (source_sheet.getRange("A" + X.toString()).getValue() === 1) {
And use triple equal signs for an equality check. JavaScript is constantly attempting to coerce variables into the type that seems correct. So, it might convert the number in the variable "X" to a string, but you can also use the toString() method.
getValues() returns a two-dimensional array. Each inner array represent a row. Each element in the inner array represents a cell in a row.
If you only want to get one value, use getValue() (no "s" on the end) instead of getValues().
var buydate = source_sheet.getRange("D" + X.toString()).getValue();
You are trying to set the value by using an equal sign. That won't work. You need to use the setValue() or setValues() method.
target_sheet.getRange("A" + Y.toString()).setValue(buydate);
By not using the var key word in your assignments, the variables automatically become "global" variables.
X = 5;
Y = 2;
Z = 1;
There's no need to make them global variables in this case, I don't think.
var X = 5,
Y = 2,
Z = 1;
You can declare multiple variables all at the same time.
<body>
<input id="input"></input>
<button id="button" onclick="evaluate()">Submit</button>
<br>
<p id="id"></p>
</body>
<script>
var a = 1;
var b = 100;
var z = Math.floor(Math.random() * (b-a)) + a;
document.getElementById("id").innerHTML = ("Pick a number between 1 and 100. I will try to guess it. I think it's " + z + ".");
var y = document.getElementById("input").value;
function evaluate() {
var y = document.getElementById("input").value;
if (y == 0) {
a = x + 1;
z = (a+b)/2;
if (z%2==1) {
z = z-0.5
}
document.getElementById("id").innerHTML = ("Now I think it's " + z);
stopEvent();
}
if (y == 2) {
b = x - 1;
z = (a+b)/2;
if (z%2==1) {
z = z-0.5;
}
document.getElementById("id").innerHTML = ("Now I think it's " + z);
stopEvent();
}
if (y == 1) {
document.getElementById("id").innerHTML = ("Yay! I'm so smart.");
stopEvent();
}
}
</script>
When I click on the button it doesn't think of another integer, it does nothing. I can't find any typos. This program is supposed to guess your number in 7 guesses. You think of a number between 1 and 100, and it first chooses a random integer 1-100, then you tell it if it's too high or too low, then it resets its range according to what you told it, and it chooses another integer, and another, until it narrows down to 1 integer.
Your variable x is never defined. You jump right into using x in a calculation when it is undefined. What is x supposed to be? Once you define x everything should work.
a = x + 1; // What is x?
If you do not define x then your function evaluate() will break.
evaluate() is a predefined method available, so change the name of your method name if you see a conflict.
Now as mentioned by mwilson, you are using variable x without declaring it with some value.
I guess you have already defined stopEvent() in your code or else you will get an error there also.
My code:
function calculate(sides) {
var sides = prompt("Triangle side lengths in cm
(number,number,number)"); //String will be size of 4
var nsides = sides.split(" "); //Splits content into array format
//Convert Array instances to integer values a,b,c
for(var loop=0;loop<=nsides.length;loop++) {
if(nsides[loop]!=",")
a = nsides[loop];
if(nsides[loop]!=",")
b = nsides[loop];
if(nsides[loop]!=",")
c= nsides[loop];
} //End for
//Area Calculation
var s = (a+b+c)*0.5 ; //represents the semiperimeter
var area = Math.sqrt(s*(s-a)*s(s-b)*(s-c)) //area calculation
//Result
sides = alert("The triangle's area is " + area + " square cm");
} //End function
//Main calculate(length);
I'm looking to set side a, b, and c to integers; however in order to do that I have to go through the array (I first converted it to an array from a string)
I'm going to add in some standard validation later; as of now I can't seem to place the values from the string entered into 3 separate integers being a b and c.
Other than that, is there a better way i can go about this?
Thanks.
Maybe I misunderstand your question, but is this what you're looking for?
var sides = prompt("Triangle side lengths in cm (number,number,number)");
var nsides = sides.split(",");
var a = +nsides[0];
var b = +nsides[1];
var c = +nsides[2];
//Area Calculation
//...
Note the use of + to force the strings from the array into numbers.
function calculate() {
var sides = prompt("Triangle side lengths in cm (number,number,number)"),
nsides = sides.split(","),
a = parseFloat(nsides[0]),
b = parseFloat(nsides[1]),
c = parseFloat(nsides[2]),
s = (a + b + c) / 2,
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
alert("The triangle's area is " + area + " square cm");
return area; // return the area
}
First of all I removed your parameter, it was totally unnecessary and was overwritten by the declaration of sides in the first line. Then I changed the split to , so it follows your instructions. Then you need to parse the string to integers using parseInt and specifiying the radix 10, then you can go on with your calculations. Just a last thing, you wrote Math.sqrt(s*(s-a)*s(s-b)*(s-c)), see that s(s-b) causes an exception because you are using a number to be called as a function.
Whilst looking for a "B-V color index to temperature conversion formula"
I found this javascript:
var C1 = 3.979145;
var C2 = -0.654499;
var C3 = 1.74069;
var C4 = -4.608815;
var C5 = 6.7926;
var C6 = -5.39691;
var C7 = 2.19297;
var C8 = -.359496;
bmv = parseFloat(BV);
with (Math) {
logt=
C1
+C2*bmv
+C3*pow(bmv,2)
+C4*pow(bmv,3)
+C5*pow(bmv,4)
+C6*pow(bmv,5)
+C7*pow(bmv,6)
+C8*pow(bmv,7);
t=pow(10,logt);
}
Which is supposed to convert B-V color index to temperature.
Does anyone understand how this is working and if the output value is an approximation for temperature in celcius or kelvin?
Is it something to do with products of logarithms?
The B-V index is basically a function that transforms the difference between the intensity of the light passing through a 'blue' resp. a 'visible' filter into a temperature.
This function can be approached as 109th degree polynomial, where the polynomial is basically C1*bv0 + C2*bv1 + ... + C8*bv7.
Since we're talking stellar temperatures, the output will be in Kelvin.
Note that a Horner algorithm for polynomial functions is often more precise...
I think temperatures are in Kelvin because its very common for astronomers to use Kelvin rather than Celcius.
Look here.
And,
if logbaseX = Y then X = baseY
log10(t)= C1 +C2*bmv +C3*pow(bmv,2) +C4*pow(bmv,3)
+C5*pow(bmv,4) +C6*pow(bmv,5) +C7*pow(bmv,6)
+C8*pow(bmv,7);
t = pow(10,log10(t));
Also, this formula is very much related to Taylor Series.
// Horners algorithm for polynomials
function Horner(a,n,x)
{
var result = a[n];
var i = n - 1;
while( i >= 0)
{
result = result*x + a[i];
i-=1;
}
return result;
}
In your case,
Array Cs = {C1,C2,C3,C4,C5,C6,C7,C8};
var temperature = Horner(Cs,8,bmv);