[SOLVED] why does this javascript function keep failing

Issue

 function greeting(name, time) {
    const greet = alert("hello" + " " + name + " " + "Good" + " " + time);
    return greeting;
  }
  greeting(Kofo, Morning);
  document.getElementById("p1").innerHTML = greeting;
</script>
<button onclick="greeting()">Click me</button>
<p id="greet"></p>

My code keeps failing and bringing back undefined

Solution

This code will do what you want to achieve.

<script>
function greeting(name, time) {
    const greet = alert("hello" + " " + name + " " + "Good" + " " + time);

  }
</script>
<button onclick="greeting('Kofo', 'Morning')">Click me</button>
<p id="greet"></p>

Answered By – ShriHans

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *