<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.
Related
I am writing JavaScript / HTML for a project for one of my classes. I'm not sure why the JavaScript function won't execute. The first return ("result") works no problem but for some reason my program wont work for ("result2"). I pasted the function down below:
function multiplyBy(){
var x = document.getElementById("text").value;
var y = document.getElementById("text2").value;
var z = x * y;
var a = 52 * z;
var b = paraseFloat(a);
if (b > 20000) {
output = "The Salary is too little."
}
if else (b < 20000; b > 25000) {
output = "The Salary is almost enough. Let's negotiate."
}
else {
output = "This is a great salary for me."
}
return document.getElementById("result").innerHTML = "The Salary is: " + b;
return document.getElementById("result2").innerHTML = output
}
You have few issues in the code
paraseFloat is a miss type it should be parseFloat
if else (b < 20000; b > 25000) {
if else should be else if
if you want to have a range in your check you need to add logical operators like && ||
and the you need to review the logic for checking the salary i am not sure but i have changed something that "has" some sense
you can check in here
function multiplyBy() {
var x = document.getElementById("text").value;
var y = document.getElementById("text2").value;
var z = x * y;
var a = 52 * z;
var b = parseFloat(a);
if (b < 20000) {
output = "The Salary is too little.";
} else if (b >= 20000 && b < 25000) {
output = "The Salary is almost enough. Let's negotiate.";
} else {
output = "This is a great salary for me.";
}
document.getElementById("result").innerHTML = "The Salary is: " + b;
document.getElementById("result2").innerHTML = output;
}
multiplyBy();
<input type='text' name='text' id='text' value=10 />
<input type='text' name='text2' id='text2' value=10 />
<div id='result'></div>
<div id='result2'></div>
I have found couple of issues in your code but you were almost there. Let me summarize in few steps where it was wrong:
In your code if else was presented. Using else if works other way around, read further here.
Logical AND operator for defining between values for salary works differently, here you can find details. b < 20000; b > 25000 is just wrongly defined, I have corrected to have b >= 20000 && b < 25000. Solution uses && and changed a bit the condition.
In parsing to float case there was a typo in your function call, should have parseFloat instead of paraseFloat. Read further here.
Just changed from b < 20000 to b > 20000 which makes more sense in terms of text result.
And lastly, in your function there are 2 return statements, even if there is no need at all in that code. The function manipulates the DOM then it will automatically return undefined. Please find here the documentation for more details which states:
A function without a return statement will return a default value. ... For all other functions, the default return value is undefined.
And finally here is a working solution:
function multiplyBy() {
const x = document.getElementById("text").value;
const y = document.getElementById("text2").value;
const z = x * y;
const a = 52 * z;
const b = parseFloat(a);
if (b < 20000) {
output = "The Salary is too little.";
} else if (b >= 20000 && b < 25000) {
output = "The Salary is almost enough. Let's negotiate.";
} else {
output = "This is a great salary for me.";
}
document.getElementById("result").innerHTML = "The Salary is: " + b;
document.getElementById("result2").innerHTML = output;
}
<input id="text" />
<input id="text2" />
<div id="result"></div>
<div id="result2"></div>
<button onclick="multiplyBy()">Calculate</button>
Additionally it is worth to read further about when to use const, let and var.
Hope this helps!
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.
I believe my input and sorting loop is correct although I'm unsure on the use of my brackets. New to java script so have lots of old java tendencies to break. Basically I want to take 3 inputs and figure out what's the largest, middle, and lowest number and if the two lower add to be greater than the largest or do not, to print the messages accordingly. Mainly just want to know what's wrong with my bracketing and my math statement. Getting errors on both my math and document.write statements. Thank you in advance.
var x = prompt("Enter your first integer: ", x);
var y = prompt("Enter your next integer: ", y);
var z = prompt("Enter your last integer: ", z);
var min = Number(min);
var med = Number(med);
var max = Number(max);
if (x > y) {
if (x > z) {
max = x;
if (y > z) {
med = y;
min = z;
} else {
med = z;
min = y;
}
} else {
med = x;
if (y > z) {
max = y;
min = z;
} else {
max = z;
min = y;
}
}
} else {
if (y > z) {
max = y;
if (x > z) {
med = x;
min = z;
} else {
med = z;
min = x;
}
} else {
med = y;
max = z;
min = x;
}
}
var sum = min + med;
if (sum > max) {
document.write("The numbers ", x ", ", y "and, ", z ",satisfy the triangle inequality");
}else(sum < max){
document.write("The numbers ", x ", ", y "and, ", z ",satisfy the triangle inequality");
It looks like the loop runs accurately, albeit with a duplicate entry (you've got two scenarios where it ends up z < x < y). Did you actually encounter any errors with this or did you just want us to check it before you ran it?
EDIT: wow, lots of activity while I wrote my answer haha! Anyways, the issue with the write statement is that you concatenate "These numbers " + X but you don't use a concatenate before ", " either time. Unless it assumes that but I don't believe it does. For the math portion, the only thing I can think of is that your else doesn't actually need the (min + med < max) because it's not an if statement. As an else it fires automatically when reached.
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.
I don't know much about JavaScript, here is the code I have:
<script language="JavaScript">
var x = 10
function startClock() {
if (x !== ' ') {
x = x - 1
document.frm.clock.value = x
setTimeout("startClock()", 1000)
}
if (x == 0) {
x = ' ';
document.frm.clock.value = x;
success.location.href = "success.php";
}
}
</script>
<body onLoad(startClock);>
affected iframe:
<input name="clock" size="3" readonly="readonly"
<iframe name="success" src="blank.htm"></iframe>
when the timer counts down, success.php is loaded twice. I know thise because 1.)It inserts data into my DB twice, 2.)I can actually see the loading symbol in the tab reloading a second.
When I change the function to something like:
<script language="JavaScript">
var x = 10
var y = 1
function startClock() {
if (x !== 'Fin') {
x = x - y
document.frm.clock.value = x
setTimeout("startClock()", 1000)
}
if (x == 0) {
x = 'Fin';
document.frm.clock.value = x;
success.location.href = "success.php";
}
}
</script>
...the page is only loaded once.
Can anyone tell me what is happening here? I also tried using '0' in place of ' ' and got the same double execution...
In Javascript there are TWO comparison operators:
"==" -- means equal to
"===" means "exactly equal to" -- which means that the value and the TYPE must be the same
I suspect (although I dind't bother to test the theory) that if you use "===" rather than "==" in your original code you will find it works as you intended. However, there are a number of things that need fixing -- 1) you are inconsistent with using ";", 2) the code should be structured to ensure that on any given iteration it can only "restart" the timer OR fire the sucess and NEVER both. Here is a cleaner version:
<script language="JavaScript">
// 10 iterations at 1 second intervals
var x = 10;
function startClock() {
document.frm.clock.value = --x;
if (x <= 0) {
document.frm.clock.value = x;
success.location.href = "success.php";
} else {
setTimeout("startClock()", 1000);
}
} // startClock
</script>
<body onLoad(startClock);>
First a couple of things. There's a number of "sloppy" coding practices in your example (missing semicolons for instance). While the code may run, it could improve with some jslint help.
So look at the case when x = 1. You decrement x so now x = 0. You then call setTimeout which will wait 1 second and then call your method named startClock. However, setTimeout doesn't block your execution. So immediately after setTimeout is called with x = 0, the code below it is executed where you set x to ' ' (and load your page). Now one second after that code has run, your method is called again due to the timer firing. Since x is now ' ', the top block is skipped and you fall into x == 0 block a second time.
Change it to:
if (x == 0) { // note this is now first
x = ' ';
document.frm.clock.value = x;
success.location.href = "success.php";
} else if (x !== ' ') { // note the else/if
x = x - 1;
document.frm.clock.value = x;
setTimeout("startClock()", 1000)
}
Otherwise, when x is 1, a timeout for startClock() will be set, AND the location will be loaded. Then, the timeout will fire, loading the page again (since x = ' ' and ' ' == 0 returns true).
It is probably better practice to say:
if (x === 0) { // note the ===
x = ' ';
document.frm.clock.value = x;
success.location.href = "success.php";
} else if (x !== ' ') {
x = x - 1;
document.frm.clock.value = x;
setTimeout("startClock()", 1000)
}
Because you don't need the truth conversion that == does for you.
Your example with 'Fin' instead of ' ' worked, because on the startClock() call after the location had been loaded, x was 'Fin', and ('Fin' == 0) is false.