I'm trying to adapt a calculator that was made by someone else who used to work at my company. Unfortunately I can't get hold of him at all and was wondering if someone here could help me. My javascript knowledge is limited so please bear with me if this sounds a stupid question or if I'm missing something obvious. I can't find any info on this elsewhere, so any help would be much appreciated!
Here is the current code for the section I want to change:
function date(){
var the_date = new Date();
enter code herevar the_year = the_date.getFullYear();
ret_age=Number(document.calculator.year.value);
gender=Number(document.calculator.sex.value);
a=the_year-ret_age;
if (a<=36)
{
document.calculator.number3.value=68;
}
else if (a>36 && a<=45)
{
document.calculator.number3.value=67;
}
else if (a>=45 && a<=60)
{
document.calculator.number3.value=66;
}
else if (a>60 && gender==1)
{
document.calculator.number3.value=65;
}
else if (a>60 && gender==0)
{
document.calculator.number3.value=65;
}
else
{
alert("Our calculator is having trouble working out your state
pension age. You have been given a default age of 68. Feel free to
change it.");
document.calculator.number3.value=68;
}
}
I want to add in these new 'else if' options, but I think that there is a problem with the number of conditions in each.
else if (a>60 && gender==0)
{
document.calculator.number3.value=65;
}
else if (a>60 && <=62 && gender==0)
{
document.calculator.number3.value=62;
}
else if (a>63 && gender==0)
{
document.calculator.number3.value=60;
}
Am I doing something wrong with trying to use eg. a<61 && <=63 alongside the gender condition? This appears to only work when I say one condition for the number, eg. a<60
Is there any way to use these two conditions in the else if, or will I have to do something different?
It would be great if someone could help - again, apologies if this question is poorly explained. If you need any more info to help then please let me know! I'm not sure what I'm doing. Thank you! :)
You missed an 'a' after '&&' on this condition: else if (a>60 && <=62 && gender==0)
The && indicates that another condition most be true in order for the if statement to validate to true. It does not apply a condition statement to the previous variable.
For example if you had two if statements as follows:
if(x > 1){
if(y < 1){
// do something
};
};
They could be combined into a singly if statement like this:
if(x > 1 && y < 1){
// do something
};
Essentially both sides of the && (or || for an "or" condition) need to be able to evaluate outside of the other statements.
So in your case as previously stated in another answer, you need to changes your if statement from:
else if (a>60 && <=62 && gender==0)
{
document.calculator.number3.value=62;
}
To:
else if (a>60 && a<=62 && gender==0)
{
document.calculator.number3.value=62;
}
your coditions inside the if statements are wrong
replace else if (a>60 && <=62 && gender==0) with else if (a>60 && a<=62 && gender==0)
Related
Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.
Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use
if ((int%3 == 0) && (int%5 == 0))
either at the end or the beginning of the block is when the changes are visible.
Note:
I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.
My solution:
for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/
else{
console.log(int);
}
}
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if blocks order is important, and you chose the wrong one.
If you remove else from else if(int%5 == 0) you will get your desired output I guess.
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
I'm new to this site and currently doing a computer programming class up in ontario, canada. I have a computer programming class and currently making an interactive tic-tac-toe game. I use if statements in a function in order to verify in theres a winner. I have an If and an else if. However only the first if works and does its code. When i try to do the conditions for do into the else if it doesn't works. However, if i swap my else if and if so that both conditions are swapped. again only first first works and second will never work. so to me it sounds like my conditions are good. i dont know if this makes sense lol
function verifie_gagnant()
{
if (document.getElementById("centregauche").firstChild.classList.contains("markX") &&
document.getElementById("centrecentre").firstChild.classList.contains("markX") &&
document.getElementById("centredroite").firstChild.classList.contains("markX") ||
document.getElementById("centregauche").firstChild.classList.contains("markO") &&
document.getElementById("centrecentre").firstChild.classList.contains("markO") &&
document.getElementById("centredroite").firstChild.classList.contains("markO"))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
else if (document.getElementById("hautgauche").firstChild.classList.contains("markX") &&
document.getElementById("hautcentre").firstChild.classList.contains("markX") &&
document.getElementById("hautdroite").firstChild.classList.contains("markX") ||
document.getElementById("hautgauche").firstChild.classList.contains("markO") &&
document.getElementById("hautcentre").firstChild.classList.contains("markO") &&
document.getElementById("hautdroite").firstChild.classList.contains("markO"))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
}
By hearing to your problem it looks like in all the cases both the condition are true. Hence which ever condition is first it executes that first skipping the else part. You can test this removing the else and keeping both in separate if condition. You will notice it navigate inside both if condition. You need to change you IF condition. See below to know more on If condition
IF is a Conditional Statements. ... Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
You have an || between && conditions which is evaluating to true in both if and else if.
So even if you swap conditions it always execute first one.
Try putting ( condition1 && condition2) || (condition3 && condition4) in both if and elseif conditions
Example fix. (You need to see where actually you need to group conditions)
function verifie_gagnant()
{
if ((document.getElementById("centregauche").firstChild.classList.contains("markX") &&
document.getElementById("centrecentre").firstChild.classList.contains("markX") &&
document.getElementById("centredroite").firstChild.classList.contains("markX")) ||
(document.getElementById("centregauche").firstChild.classList.contains("markO") &&
document.getElementById("centrecentre").firstChild.classList.contains("markO") &&
document.getElementById("centredroite").firstChild.classList.contains("markO")))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
else if ((document.getElementById("hautgauche").firstChild.classList.contains("markX") &&
document.getElementById("hautcentre").firstChild.classList.contains("markX") &&
document.getElementById("hautdroite").firstChild.classList.contains("markX")) ||
document.getElementById("hautgauche").firstChild.classList.contains("markO") &&
document.getElementById("hautcentre").firstChild.classList.contains("markO") &&
document.getElementById("hautdroite").firstChild.classList.contains("markO")))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
}
This is the first thing you can fix (syntax) after this you need to actually see what is the actual data returned after the single condition is evaluated. Maybe both have same data.
I have what I thought would be a simple logic check. In my code
$scope.seatMap.PlaneTypeCode = "175"
However, when I set
$scope.seatMap.PlaneTypeCode === "175" //my debugger returns <b>false </b>
parseInt($scope.seatMap.PlaneTypeCode,10) ===175 // equals 17
I added a few zeros on the radix but that did nothing to help.
I am not sure how to do a comparison check. Any insight on this would be hugely appreciated.
Here is my full if statement
if (parseInt(col.name,10) ===4 && parseInt($scope.seatMap.PlaneTypeCode,10) ===175 && $scope.TripSummary) {
col.available = false;
}
****** Changed my response to this
if (parseInt(col.name,10) ===4 && $scope.seatMap.PlaneTypeCode ==="175" && $scope.TripSummary) {
col.available = false;
} // still getting false
=== is a best practice, you should use it. Review the reference provided by #Joyson
You don't need the ,10 in parseInt because it is the default.
var PlaneTypeCode = "175";
if (parseInt(PlaneTypeCode) === 175) {
console.log('equal');
}
If PlaneTypeCode is a code and can contain anything other than digits, a better comparison would be:
if (PlaneTypeCode === "175")
You can use == instead of ===
$scope.seatMap.PlaneTypeCode == "175"
Please refer to Difference between == and === to know more
use angular.equals($scope.seatMap.PlaneTypeCode,"175")
This code seems to loop through adding 1 to player1.score untill the score is === to whatever i put in the second if statement. Anyone know why?
pointScored: {
startNextSet: function(Scorer) {
if (gameController.bananasTaken < 3 && Scorer === "player1") {
console.log(gameController.player1.score);
gameController.player1.score += 1;
if (gameController.player1.score === 10 &&
gameController.bananasTaken === 0 &&
gameController.player1.bananaCount === 0) {
console.log(gameController.player1.score);
gameController.updatePlayerStats(gameController.Banana1, 20, gameController.canvas.height
- 20 - gameController.Banana1.height, gameController.player1, "left");
console.log("player 1's first point");
}
I'm currently learning about using a debugger but thought i'd leave this here to see if anyone knows why. Thanks.
There's a chance your values get evaluated as strings. The === operator doesn't do any type conversions, that's why its faster.
Consider changing your evaluation to use ==. The same issue has cropped up in another question.
I have refactored your code a bit & used the == notation I suggest above. Please try running it and tell me if it works.
pointScored:{
startNextSet: function(Scorer) {
gc=gameController; //to save thy fingers from typing ache
if (gc.bananasTaken > 2 || Scorer !== "player1")
return;
console.log(gc.player1.score); // this logs 6 times from 0 to 5
gc.player1.score += 1;
if (gc.player1.score == 5 && gc.bananasTaken == 0) {
alert(gc.player1.score); //*******!
if(gc.player1.bananaCount == 0) {
gc.updatePlayerStats(gc.Banana1, 20, gc.canvas.height - 20 - gc.Banana1.height, gc.player1, "left");
console.log("player 1's first point");
}
}
}
}
As I look at your function, it seems that this logic needs to be INSIDE the gameController object.
Code:
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value!GenderField.value) {
alert('No criteria Added');
return;
}
The alert is not called when all the text fields are blank.
You're missing the && between the last two criteria
It should be:
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value && !GenderField.value)
In cases like this, it makes a lot of sense to format your if statement like this:
if ( !IDTextField.value
&& !FirstNameField.value
&& !LastNameField.value
&& !DateOfBirthField.value
&& !GenderField.value)
If you do it this way, you can't make the mistake you just made.
xbonez got it right. You are missing && between last two expression.
For something not so important, I would like to get all expressions evaluated using || and then add negation using !, rather than negating all expression and evaluate them with &&. This can make this expression a little faster, if am not wrong.
if (!(IDTextField.value || FirstNameField.value ||
LastNameField.value || DateOfBirthField.value || GenderField.value)) {
alert('No criteria Added');
return;
}
Tell me what you all think??
What is this little abomination?
... !DateOfBirthField.value!GenderField.value
I think that should be:
... !DateOfBirthField.value && !GenderField.value
You should write your code like this
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value && !GenderField.value) {
alert('No criteria Added');
return;
}
You're missing the && between the last two criteria
the && is missed ,add it and try ,should work if no other errors exist