Help on if/Else Statement

Hi,

I`m a beginner on Java. Dealing with a exercise I wrote this code:

var weather = ‘rainy’;
var isHappy = true;
if (weather=== ‘rainy’&& isHappy===true){
console.log (‘I am singing in the rain, I am singing in the rain!’);
}else{
console.log(‘a strain’);}

The exercise is to “add an else statement. Inside the code block of the else statement, console.log() a string”.

What is wrong with the code? Is the isHappy should be false on else block?

Thanks in advance

Hi,
this looks like Javascript, not Java.
Change isHappy to false and see if ‘a string’ is logged to the console.

Hello again and thanks,

What I don´t understand is the condition on else statement. Because I have var weather and var isHappy=true on if statement, but on else I don´t have any condition.

On the print is show only a strain and not the 2 alternatives, the if and else statements.

It´s possible to rewrite the code and the 2 statements (if and else) could appear?

var weather = ‘rainy’;
var isHappy = true;
if (weather=== ‘rainy’&& isHappy===true){
console.log (‘I am singing in the rain, I am singing in the rain!’);

\rewrite after this line//

}else{
console.log(‘a strain’);}

Thanks

Note that ‘strain’ is not the same as ‘string’. So if you write anything else than the lesson tells you to, you’ll fail the test.

The else statement without any condition catches all conditions that weren’t covered by the preceding conditions.
If you want the else statement to be bound to a condition, you can write else if(condition).

like this??
var weather = ‘rainy’;
var isHappy = true;
if (weather=== ‘rainy’&& isHappy===true){
console.log (‘I am singing in the rain, I am singing in the rain!’);
}else {if(weather===‘rainy && isHappy===false’)
console.log(‘a string’);}

The only reads the first part of code

The syntax is as follows:

if(condition) {
// if the condition is met, go here only
}
else if(another condition){
// if the upper condition is not met but this is, go here only
}
else{
// in any other case, go here
}