Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I would like to use different formulas based on a number entered. For example, if a number is greater than 25,000,000, one computation will be performed. If a number less than 25,000,000 but greater than 10,000,000, another computation will be performed, etc.
Any suggestions on how to make the following work:
HTML:
<input type="text" id="calculation" name="submitted[calculation]" maxlength="20" class="form-text required">
jQuery:
$("#calculation").change(function () {
if (parseFloat(this.value) >= 25000000) {
alert("court determines rate");
if (parseFloat(this.value)( < 25000000 && >= 10000000)) {
alert(113000 + (((this.value) - 10000000) * 0.005));
if (parseFloat(this.value)( >= 1000000 && < 10000000)) alert(23000 + (((this.value) - 1000000) * 0.01));
if (parseFloat(this.value)( >= 200000 && < 1000000)) alert(7000 + (((this.value) - 200000) * 0.02));
if (parseFloat(this.value)( >= 100000 && < 200000)) alert(4000 + (((this.value) - 100000) * 0.03));
else {
alert((this.value) * .04)
}
})
});
UPDATE: I've updated the code (again) as follows, but am still not having any luck. Any constructive suggestions?
$("#calculation").change(function () {
var entry = parseFloat(this.value);
if (entry >= 25000000) {
alert("court determines rate");
}
else if (entry >= 10000000)) {
alert(113000 + (((this.value) - 10000000) * 0.005));
}
else (entry >= 1000000) {
alert(23000 + (((this.value) - 1000000) * 0.01));
}
else (entry >= 200000) {
alert(7000 + (((this.value) - 200000) * 0.02));
}
else (entry >= 100000) {
alert(4000 + (((this.value) - 100000) * 0.03));
}
else {
alert((this.value) * .04);
}
})
})
The syntax you used above is very hard to read and is invalid. You should only use parseFloat once and not use it everywhere. Here is a very simple example of what you should be doing:
$("#calculation").change(function () {
var parsedFloat = parseFloat(this.value);
if (parsedFloat > 25000000){
console.log('use formula 1. Number is greater than 25000000');
}
else if (parsedFloat > 10000000){
console.log('use formula2. Number is greater than 10000000 and less than or equal to 25000000');
}
else {
console.log('use formula3. Number is less than or equal to 10000000');
}
});
After some more trial and error, I figured out the problem. There was an extra end parenthesis within the first elseif.
Here is the corrected code:
$(document).ready(function () {
$("#calculation").change(function () {
var entry = parseFloat(this.value);
if (entry >= 25000000) {
alert("court determines rate");
} else if (entry >= 10000000) {
alert(113000 + (((this.value) - 10000000) * 0.005));
} else if(entry >= 1000000) {
alert(23000 + (((this.value) - 1000000) * 0.01));
} else if (entry >= 200000) {
alert(7000 + (((this.value) - 200000) * 0.02));
} else if (entry >= 100000) {
alert(4000 + (((this.value) - 100000) * 0.03));
} else {
alert((this.value) * .04);
}
});
});
Related
I need to solve the following problem:
Create class Intern with next fields:
Name(String)
Surname(String)
laziness(number from 20 to 50)
Curiosity (number from 0 to 100)
Skill (number from 0 to 100)
Irresponsibility (float number from 0.0 to 1.0)
For this class create method that calculate “mark” for intern, that is calculated by formula(in the code)
It looks simple, but when I run it, the output is NaN. Could somebody help please?
class Intern {
constructor(name, surname, laziness, curiosity, skill, irresponsibility) {
this.name = name
this.surname = surname
if (laziness <= 50 && laziness >= 20) {
this.laziness = laziness
} else {
this.laziness = 0
}
if (curiosity <= 100 && curiosity >= 0) {
this.curiosity = curiosity
}
if (skill <= 100 && skill >= 0) {
this.skill = skill
}
if (irresponsibility <= 0 && irresponsibility >= 1) {
this.irresponsibility = irresponsibility
}
}
getMark() {
let mark = (((this.skill + this.curiosity) * (1.5 - this.irresponsability)) / (this.laziness * 0.25));
return mark
}
}
You've misspelled the irresponsibility variable in the getMark() method, and the if statement in your constructor for it will never be true:
if (irresponsibility <=0 && irresponsibility >=1)
I think you were meaning to say:
if (irresponsibility >=0 && irresponsibility >=1)
I am creating a game that guesses the names from the images. There are 10 rounds in each game and after the 10 rounds there is a bonus round which you have to guess how old they are and you wager your points. You have to guess within 2 years of their age. If you guess right then the amount you wagered is added to your existing score. If it is incorrect then you score is decreased by the amount you wagered.
If i guess 2 below the actual age it adds up correctly.
If i guess the exact age it adds up correctly.
BUT if I guess 2 years above the actual age it decreases the score which it is not supposed to do. I don't know what i am doing wrong.
Here's a preview of my code:
var age = calcAge(celeb_q_a[celeb_q_a.length-1].dob);
age = age.toFixed(0);
alert(age);
var user_age = document.getElementById("answer-age").value;
var prev_score = score * 10;
if ((((age - 2) == user_age)) || (age == user_age) || ((2 + age) == user_age)) {
prev_score += (document.getElementById("wage").value * 1);
}else{
prev_score -= (document.getElementById("wage").value * 1);
}
I would suggest changing to a pair of "less than or equal" and "greater than or equal" to include all values between age+2 and age-2 respectively. I.e.
if ((age + 2) <= user_age && (age - 2) >= user_age) {
// do stuff
}
You can simplify the condition by testing for the ages that fail:
if ((age - 2) < user_age) || ((age + 2) > user_age) {
prev_score -= (document.getElementById("wage").value * 1);
} else {
prev_score += (document.getElementById("wage").value * 1);
}
You have to guess within 2 years of their age
You can simplify your ranged validation using below code:
/* run tests */
console.log(isAnswerCorrect(17, 20, 2)) // expect false
console.log(isAnswerCorrect(18, 20, 2)) // expect true
console.log(isAnswerCorrect(19, 20, 2)) // expect true
console.log(isAnswerCorrect(20, 20, 2)) // expect true
console.log(isAnswerCorrect(21, 20, 2)) // expect true
console.log(isAnswerCorrect(22, 20, 2)) // expect true
console.log(isAnswerCorrect(23, 20, 2)) // expect false
/* checker */
function isAnswerCorrect(guess, actual, range) {
if (guess < actual - range || actual + range < guess) {
return false
} else {
return true
}
}
When I run this code, only the INVALID (over 100) and High Distinction works. Any number below 80 also shows High Distinction. What have I done wrong?
function calculateGrade() {
var fvalue = Number(prompt('Please enter final score for unit. Enter a whole number only'));
document.write('The final score entered is ' + fvalue + '<br />');
if (fvalue > 100) {
document.write('INVALID');
} else if (80 <= fvalue <= 100) {
document.write('High Distinction');
} else if (70 <= fvalue <= 79) {
document.write('Distinction');
} else if (60 <= fvalue <= 69) {
document.write('Credit');
} else if (50 <= fvalue <= 59) {
document.write('Pass');
} else if (0 <= fvalue <= 49) {
document.write('Fail');
} else if (fvalue < 0) {
document.write('INVALID');
}
}
calculateGrade()
Your comparison syntax is invalid. You need to check one boundary at a time:
if (80 <= fvalue && fvalue <= 100) {
Same for the others.
To take it a step further, you only need to check one boundary, because the higher end is excluded by the else:
if (fvalue > 100) {
document.write('INVALID');
} else if (80 <= fvalue) {
document.write('High Distinction');
} else if (70 <= fvalue) {
// ...
This isn't java.
But you can surely try this.
else if ( (fvalue >= 80) && (fvalue<= 100)) {
document.write('High Distinction');
This question already has answers here:
How does (A == B == C) comparison work in JavaScript?
(6 answers)
Closed 6 years ago.
today I decided I wanted to make a simple js code that would accept a number (in meters), and decide what the appropriate metric unit to use would be. The code turned out to be a little more complicated than I had expected, but I was able to figure out most of the bugs as I found them (even if it meant rearranging all of my code). However, when it came to my if/else statement I could not figure it out. If I put in a number that is less than 1 nothing happens. If I put in a number more than 9 it logs the same thing every time. The structure itself may need some work, but if someone could help me with the if/else statement I would be very thankful. Here is the code (init is called when the body loads):
function init() {
var x = prompt("How many meters?");
convertMetricMeters(x);
function convertDown(x) {
if (0.1 >= x >= 0.99) {
console.log("deci");
}
else if (0.100 >= x >= 0.999) {
console.log("centi");
}
else if (0.1000 >= x) {
console.log("milli");
}
else {
console.log("error");
}
}
function convertUp(x) {
if (1 <= x <= 99) {
console.log("deca");
}
else if (100 <= x <= 999) {
console.log("hecto");
}
else if (1000 <= x) {
console.log("kilo");
}
else {
console.log("error");
}
}
function convertMetricMeters(x) {
if (x < 1) {
convertDown(x);
}
else if (x > 9) {
convertUp(x);
}
else {
console.log("Appropriate Metric Unit");
}
}
}
Use && as AND operator in javascript
Convert these 100 <= x <= 999 to 100 <= x && x <= 999
You could simplify the check a bit and return if a condition is true.
function convertDown(x) {
if (x < 0.01) {
console.log("milli");
return;
}
if (x < 0.1) {
console.log("centi");
return;
}
if (x < 1) {
console.log("deci");
return;
}
console.log("error");
}
Your code has 2 sort of errors. One was simple to fix, that you have to add && between two conditions in if statement.
Now coming to the other part, the less than 1 items. It needed a different logic. Well, your maths seems to be needing bit attention. 0.1 is same as 0.100 and is same as 0.1000
I have updated the code to look for the number of digits after the decimal point and then console.log accordingly.
The updated code will be:
function init() {
var x = prompt("How many meters?");
convertMetricMeters(x);
function convertDown(x) {
// checks the number of digits after decimal point
decimals = (x.split('.')[1] || []).length
if (decimals == 1 || decimals == 2) {
console.log("deci");
}
else if (decimals == 3) {
console.log("centi");
}
else if (decimals == 4) {
console.log("milli");
}
else {
console.log("error");
}
}
function convertUp(x) {
if (1 <= x && x <= 99) {
console.log("deca");
}
else if (100 <= x && x <= 999) {
console.log("hecto");
}
else if (1000 <= x) {
console.log("kilo");
}
else {
console.log("error");
}
}
function convertMetricMeters(x) {
if (x < 1) {
convertDown(x);
}
else if (x > 9) {
convertUp(x);
}
else {
console.log("Appropriate Metric Unit");
}
}
}
Working jsfiddle example: https://jsfiddle.net/w7pf3moL/
A simplified version with only 1 method convert(float x):
function init() {
var x = prompt("How many meters?");
convertMetricMeters(x);
function convert(x) {
if (x < 0.01) console.log("milli");
else if (x < 0.1) console.log("centi");
else if (x < 1) console.log("deci");
else if (x < 10) console.log("meter");
else if (x < 100) console.log("deca");
else if (x < 1000) console.log("hecto");
else console.log("kilo");
}
function convertMetricMeters(x) {
if (x > 0) {
convert(x);
} else {
console.log("Appropriate Metric Unit");
}
}
}
init();
I am very new to programming so please forgive my perhaps dumb question. I have built three functions that calculate tax rates based upon the same input "amount". I am trying to figure out a way that I could have a user enter in the amount once and get the return from all three functions. Here they are below.
//Function 1
var normalrtfCalculator = function (amount) {
if (amount <= 150000) {
return Math.ceil(amount / 500) * 2;
} else if (amount <= 350000) {
if ((amount - 150000) <= 50000) {
return 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
} else {
return 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
}
} else {
if ((amount - 200000) <= 350000) {
return 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
} else if ((amount - 550000) <= 300000) {
return 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
} else if ((amount - 850000) <= 150000) {
return 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
} else {
return 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
}
}
};
//Function 2
var mansionTax = function (amount) {
if (amount > 1000000) {
return amount * 0.01;
}
};
//Function 3
var lowincomertfCalculator = function (amount) {
if (amount <= 350000) {
if (amount <= 150000) {
return (Math.ceil(amount / 500)) * 0.5;
} else {
return 150 + (Math.ceil((amount - 150000) / 500)) * 1.25;
}
} else {
if ((amount - 150000) <= 400000) {
return 420 + (Math.ceil((amount - 150000) / 500) * 2.15);
} else if ((amount - 550000) <= 300000) {
return 2140 + (Math.ceil((amount - 550000) / 500) * 2.65);
} else if ((amount - 850000) <= 150000) {
return 3730 + (Math.ceil((amount - 850000) / 500) * 3.15);
} else {
return 4675 + (Math.ceil((amount - 1000000) / 500) * 3.4);
}
}
};
Just have a function that runs the other ones and returns an object of the results:
var calculateTax = function(amount){
return {
rtf : normalrtfCalculator(amount),
mansion: mansionTax(amnount),
lowincome: lowincomertfCalculator(amount)
}
}
then you can call it like this:
var tax = calculateTax(99999);
To get the individual results you then just access the properties:
alert(tax.rtf), alert(tax.mansion) and alert(tax.lowincome)
The minimum change required could be as little as adding () before assigning to the variables.
Something like :
var amount = 100;
//Function 2
var mansionTax = (function () {
if (amount > 1000000) {
return amount * 0.01;
}
})();
Here is a working sample.
This will remove the need for defining any additional functions.