Math and loop issue - javascript

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.

Related

NodeJs calculation gone wrong

I have if statement like:
if((gotPrice * price.value).toFixed(0) >= answers.MINIMUM_BUY_AMOUNT) {
...
}
Results are
(gotPrice * price.value).toFixed(0) = 0
and
answers.MINIMUM_BUY_AMOUNT = 200
Then it fall to true! not sure in what world 0 is greater or equal to 200!!
I also tried this way but results was the same
if((gotPrice * price.value).toFixed(0) >= Number(answers.MINIMUM_BUY_AMOUNT).toFixed(0)) {
...
}
sample:
var x = 0.3431;
var y = 1.5467;
var z = '200';
console.log((x * y).toFixed(0) >= z); // false (but in my case says true!)
Any suggestions?
ToFixed converts number to string, calculations should be done with numbers
var x = 0.3431;
var y = 1.5467;
var z = parseInt('200');
console.log(Math.round(x * y) >= z);

Not sure why this JavaScript function is not working

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!

How to calculate the square root without using library and built-in methods in Javascript?

Please help me to write a function to compute the square root of positive real numbers using the formula:
x i+1 = (1/2) * (xi + (A / x1)),
where 'A' - input real number.
On the zero iteration next statements have been taken x0 = A
The error should be at least 10-6
Output
sqrt (2) = 1.414
sqrt (9) = 3
sqrt (25) = 5
You could take xi (x) and the new value of xi + 1 (x1) and check if the values are equal. Then end the series and return that value.
For starting, you need an apporopriate value like the half of the given value.
function sqrt(a) {
var x,
x1 = a / 2;
do {
x = x1;
x1 = (x + (a / x)) / 2;
} while (x !== x1);
return x;
}
console.log(sqrt (2)); // 1.414
console.log(sqrt (9)); // 3
console.log(sqrt (25)); // 5
You can also use bisection - a more general method for solving problems:
var sqrt = function(n) {
if (n<0) {
throw "are you kidding?! we are REAL here.";
}
if (n === 0) {
return 0;
}
var bisect = function(l,r) {
var avg = (l+r)/2;
if (r-l<0.00000001) {
return (l+r)/2;
}
if (avg*avg > n) {
return bisect(l, avg);
} else if (avg*avg < n) {
return bisect(avg, r);
}
}
return bisect(0, n < 1 ? 1 : n);
}

JS code in multi-condition and multi-increment is not working, I wondering what's wrong with it

I tried to output the total and even number for x and y as below, but not working. I checked on the firebug to find the error and it said "Error: An attempt was made to use an object that is not, or is no longer, usable"
Hope you understand what is asking. Thanks in advance.
var total = even = 0;
for (var x = 1, y = 1; x <= 10; x++, y++) {
if ((y % 2) == 0) {
even += y;
}
total += x;
}
document.write("Total : " + total + " even : " + even);

The button doesn't work

<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.

Categories