Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Not getting a return for the If...Else statement I am writing.

So I am following a set of instructions from a ebook as a tutorial to make a program for a website.

However, it seems I am getting stuck on the conditions for if.. Else statements.

IMG of instructions here.
Spoiler (Click to View)


this is the current code that I have written.

Quote: var cards = ["Queen", "Queen", "King", "King"];

var cardsInPlay = [];

// Variables assigned cards

var cardOne = cards[0];
var cardTwo = cards[1];
var cardThree = [];
var cardFour = [];

// Pushing the elements into the "cardsInPlay variable"

cardsInPlay.push(cardOne);
cardsInPlay.push(cardTwo);

if (cardsInPlay.length === 2) {
console.log("you have selected two cards");
} else if (cardsInPlay[0] === cardsInPlay[1]) {
alert("You found a match!");
}



Can anyone help?
(Apr 29, 2017 04:46 PM)Kuso Wrote:  So I am following a set of instructions from a ebook as a tutorial to make a program for a website.

However, it seems I am getting stuck on the conditions for if.. Else statements.

IMG of instructions here.
Spoiler (Click to View)


this is the current code that I have written.



Can anyone help?

Should be == not === lol.

Also, it should be a nested if statement. You have it as "else if" currently, which means that code will run if the length of the cardsInPlay array is NOT 2 as "else if" only runs if the original "if" condition is not met, meaning whenever your length is 2, you will never be running the else if.

Below is the corrected code:

if (cardsInPlay.length === 2) {
console.log("you have selected two cards");
if (cardsInPlay[0] === cardsInPlay[1])
alert("You found a match!");
else
alert("Sorry, try again");
}
This is what the text asked me to do:

Code
if (cardsInPlay.length === 2){
    if (cardsInPlay[0] === cardsInPlay[1])
        alert("You found a match!");
    else
        alert("Sorry, try again.");
}


(Apr 29, 2017 04:48 PM)SwordofJustice Wrote:  Should be == not === lol.

Also, it should be a nested if statement. You have it as "else if" currently, which means that code will run if the length of the cardsInPlay array is NOT 2 as "else if" only runs if the original "if" condition is not met, meaning whenever your length is 2, you will never be running the else if.

Below is the corrected code:

if (cardsInPlay.length === 2) {
console.log("you have selected two cards");
if (cardsInPlay[0] === cardsInPlay[1])
alert("You found a match!");
else
alert("Sorry, try again");
}

What should be "==" and not "===" ?
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
(Apr 29, 2017 05:29 PM)SIM-429621 Wrote:  This is what the text asked me to do:

Code
if (cardsInPlay.length === 2){
    if (cardsInPlay[0] === cardsInPlay[1])
        alert("You found a match!");
    else
        alert("Sorry, try again.");
}



What should be "==" and not "===" ?

Appreciate the input and help.

but what this user said ^

in learning where I am at, I have not seen "==" as I have seen "===" and "!==".

EDIT: Also, I noticed that the line of code you wrote actually works in replacement, what is the correct way of using it when it comes to "If...Else"? As stated in the instructions, it says to use if Else in the second line of conditions.
(Apr 29, 2017 06:24 PM)Kuso Wrote:  Appreciate the input and help.

but what this user said ^

in learning where I am at, I have not seen "==" as I have seen "===" and "!==".

EDIT: Also, I noticed that the line of code you wrote actually works in replacement, what is the correct way of using it when it comes to "If...Else"? As stated in the instructions, it says to use if Else in the second line of conditions.

It's a check condition inside a check condition, under 11 part 1, it says to make an if else statement inside the first if statement you made. If you have a specific questions say it. Also this should be here:

Web Development

As it's JS and not Java.
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
(Apr 29, 2017 06:57 PM)SIM-429621 Wrote:  It's a check condition inside a check condition, under 11 part 1, it says to make an if else statement inside the first if statement you made. If you have a specific questions say it. Also this should be here:

Web Development

As it's JS and not Java.

if (cardsInPlay.length === 2) {
console.log("you have selected two cards");
} else if (cardsInPlay[0] === cardsInPlay[1]) {
alert("You found a match!");
}
else
alert("Sorry, try again");
};

would be correct? I am not getting a alert after I put the "else if" on the second condition as I did before doing so. I don't am looking it up now, but so far I don't see it.

Also, yeah I just noticed. I was trying rush getting some things done including this and didn't pay close enough attention lol.
(Apr 29, 2017 09:08 PM)Kuso Wrote:  if (cardsInPlay.length === 2) {
console.log("you have selected two cards");
} else if (cardsInPlay[0] === cardsInPlay[1]) {
alert("You found a match!");
}
else
alert("Sorry, try again");
};

would be correct? I am not getting a alert after I put the "else if" on the second condition as I did before doing so. I don't am looking it up now, but so far I don't see it.

Also, yeah I just noticed. I was trying rush getting some things done including this and didn't pay close enough attention lol.

No that's not correct, because you are saying if there are 2 cards in the array, just print in console "you have selected 2 cards", and then ELSE IF (as in, if there are NOT 2 cards in the array) compare card 1 and card 2.

Thats why you need to make if and check if there are 2 cards in the array, and then put the if else inside the {} of the first if, so it would say this:

if (thereAre2CardsInTheArray){ // If there are 2 cards in the array do the following code between {}

if (card1 === card2) // if both cards are the same
alert("You have a match!"); // show a message with "You have a match
else // and if the 2 cards are not equal
alert("Sorry, try again."); // show a message with "Sorry, try again."
// end of the if-else statement
} // end of the if statement

Note that if there is only one condition to execute in your "if" or "if-else" or "if-else if-else" statement you don't have to put {}, but you can.

For example:

if (userAge > 18)
alert("You are a grown up !");

and


if (userAge > 18) {
alert("You are a grown up !");
}

Will have the same output, but don't forget that you can use the first one only if there is 1 statement you want to execute if the specified condition in () is true, if there is more than one statement then you use {}.
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
I would personally use "===" rather than "==" in this situation because you're looking for the specific type (int)

If you'd like, I'll send you a PM and I can sort this out for you.
If you always do what you’ve always done, you’ll always get what you’ve always got.
(Apr 30, 2017 03:41 AM)Lucifer's Inferno Wrote:  I would personally use "===" rather than "==" in this situation because you're looking for the specific type (int)

If you'd like, I'll send you a PM and I can sort this out for you.

He is using strict equality, I don't know what SwordOfJustice was talking about.
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
https://www.w3schools.com/js/js_if_else.asp

https://www.w3schools.com/jsref/jsref_if.asp

https://developer.mozilla.org/ro/docs/We.../if...else

http://www.tutorialsteacher.com/javascri...-condition
Wrong section, this is the Java and not the JavaScript section.
Nest the if statement.

Code
if (cards.length === 2) {
    if (cards[0] === cards[1])
        alert("You found a match");
    else
        alert("Try again");
}

Or have I misunderstood?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by blackcoder00Mar 11, 2019 07:20 AM 5 245 Mar 11, 2019 07:20 AM
Last Post: blackcoder00
[Java] by UnturnedDec 9, 2015 08:03 AM 1 125 Dec 9, 2015 08:03 AM
Last Post: +Haze
[Java] by Woe Is MeOct 9, 2015 04:27 PM 5 133 Oct 9, 2015 04:27 PM
Last Post: Astigmatism