My if- else is not working

Joined
Oct 25, 2023
Messages
1
Reaction score
0
I am trying to make an easy if- else statement in javascript that displays different greetings depending on the time. I can not see what I have done wrong, but only the else-statement shows, no matter what time it is. Please help!

(Here is my script. And I live in Sweden where we have 24 hour clock so that is why it is 20 and so fourth)

Skärmklipp.PNG
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
Hello !

it's not your IF/ELSE IF serial which cause trouble.
It's the ending statement, the function innerHtml requires 1 Html TAG inside the string.
look at innerText too , it's design for 'verbals'
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
the function innerHtml requires 1 Html TAG inside the string
AFAIK it's not true, you can use innerHTML without html tags.

very simple example
HTML:
<p></p>
<p></p>
<p></p>
<hr>
<p></p>
<p></p>
<p></p>

<script>
  const p = document.querySelectorAll('p');
  p[0].innerHTML = 'Lorem to innerHTML';
  p[1].innerText = 'Lorem to innerText';
  p[2].textContent = 'Lorem to textContent';
  // ----------------------------------------------
  p[3].innerHTML = '<b>Lorem to innerHTML</b>';
  p[4].innerText = '<b>Lorem to innerText</b>';
  p[5].textContent = '<b>Lorem to textContent</b>';
</script>

mistake is on the end method getHours, missing parentheses.
const time = new Date().getHours();

HTML:
<p id="greeting"></p>

<script>
  function greeting() {
    const time = new Date().getHours();
    let greeting;

    if (time < 6)
      greeting = 'time < 6';
    else if (time < 10)
      greeting = 'time < 10';
    else if (time < 20)
      greeting = 'time < 20';
    else if (time < 22)
      greeting = 'time < 22';
    else if (time < 23)
      greeting = 'time < 23';
    else
      greeting = 'WHAT! WHY ARE YOU STILL NOT SLEEPING?! ...';

    //console.log(greeting);
    document.querySelector('#greeting').textContent = greeting;
  }

  greeting();
</script>
If you not using in text html tags I recommend use textContent.

innerHTML vs. innerText vs. textContent
 
Last edited:
Joined
Oct 19, 2023
Messages
3
Reaction score
0
I am trying to make an easy if- else statement in javascript that displays different greetings depending on the time. I can not see what I have done wrong, but only the else-statement shows, no matter what time it is. Please help!

(Here is my script. And I live in Sweden where we have 24 hour clock so that is why it is 20 and so fourth)

View attachment 2697
You never initialized the variable 'greeting'.

You should have the following code:

let greeting = ""
 
Last edited:
Joined
Jul 4, 2023
Messages
366
Reaction score
41
You never initialized the variable 'greeting'.
AFAIK Javascript allows this type of variable initialization
JavaScript:
let greeting;

even treat (initialize) values for the same variable this way,
JavaScript:
let greeting;
console.log(typeof greeting, greeting);

greeting = 1.2;
console.log(typeof greeting, greeting);

greeting = "1"; 
console.log(typeof greeting, greeting);

although this example is a demonstration of poor programming practices
JavaScript:
greeting = 1.2;
console.log(typeof greeting, greeting); // number

greeting = "1"; 
console.log(typeof greeting, greeting); // string
you should avoid using the same variable to store values of different types


[ JavaScript Variables can be declared in 4 ways ]
 
Last edited:
Joined
Oct 19, 2023
Messages
3
Reaction score
0
I shouldn't have suggested

let greeting = ""

I should have suggested

let greeting = 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top