-1

I got a task to make the user able to enter a wind strength in m/s, and the application must then show how many watts produced per hour. Example: If the user states 3.7 m/s, it should print that the windmill produces approximately 10 watts per hour. The task is not important for u guys, i just wanted to give context. The problem i have is that the program does not run, but i do not see the problem. Thank you :)

Here is the code:

function myFunction() {

  var strength selected = document.getElementById("wind strength").value;

  if (strengthselected == 1.6 - 3.3) {
    document.getElementById("output value").innerHTML = "The wind turbine produces about 2 watts per hour";
  } else if (strengthselected == 3.4 - 5.4) {
    document.getElementById("output value").innerHTML = "The wind turbine produces about 10 watts per hour";
  } else if (strengthselected == 5.5 - 7.9) {
    document.getElementById("output value").innerHTML = "The wind turbine produces about 60 watts per hour";
  } else if (strengthselected == 8 - 10.7) {
    document.getElementById("output value").innerHTML = "The wind turbine produces about 150 watts per hour";
  } else if (strengthselected == 10.8 - 13.8) {
    document.getElementById("output value").innerHTML = "The wind turbine produces about 400 watts per hour";
  } else if (strengthselected == 13.4 - 17.1) {
    document.getElementById("output value").innerHTML = "The wind turbine produces about 50 watts per hour";
  } else {
    document.getElementById("out value").innerHTML = "The wind turbine produces 0 watts per hour";
  }

}
<body>

  <input type="number" id="wind strength" placeholder="Enter wind strength in m/s">

  <button onclick="myFunction"> Press </button>

  <p id="out value"> </p>


</body>

I am new to coding and js in general, so i do not really know about any other options or other ways to go at this problem.

What i am expecting is the code to work and that the words "the wind turbine produces about x watts per hour - depending on what numbers the user submitted in the input element" display on the html document.

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
chacha32
  • 1
  • 1
  • 2
    When the button is clicked, it will evaluate the contents of the `onclick` property. Change the value from `myFunction` to `myFunction()`, otherwise the function will not be called. Ids and variable names cannot have spaces and the id of the `

    ` is `out value`, not `output value`.

    – I'mAUserNow Mar 17 '23 at 16:16
  • 1
    `strengthselected == 1.6 - 3.3` tests whether `strengthselected` is equal to the result of the calculation `1.6 - 3.3` (which is always `-1.7`). It does *not* test whether it's within a range, which is what I assume you're attempting here. – deceze Mar 17 '23 at 16:18
  • 1
    Variable wrong... ``var strength selected`` is wrong. it should be ``var strengthselected`` – Not A Bot Mar 17 '23 at 16:18
  • 1
    Do not use whitespace in ids.... Got error in your variable name. ` "message": "Uncaught SyntaxError: Unexpected identifier 'selected'",` Install a linter on your IDE – epascarello Mar 17 '23 at 16:18
  • 1
    Also, var strength selected needs to be ONE word, no spaces. – Ozone Mar 17 '23 at 16:19
  • [id's value must not contain whitespace](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) – Mister Jojo Mar 17 '23 at 16:25

1 Answers1

-2

Use onclick="myFunction()" instead of onclick="myFunction"

Note: You can not use two id in one element out value use out-value, when you add a space between two elements in an id it considers it as two id's

Note: I don't know what you are trying to achieve but all your if statements are invalid

strengthselected == 1.6 - 3.3 is invalid

Demo

function myFunction() {

  var strengthselected = document.getElementById("wind-strength").value;

  var outValue = document.getElementById("out-value");

  if (strengthselected == 1.6 - 3.3) {
    outValue.innerHTML = "The wind turbine produces about 2 watts per hour";
  } else if (strengthselected == 3.4 - 5.4) {
    outValue.innerHTML = "The wind turbine produces about 10 watts per hour";
  } else if (strengthselected == 5.5 - 7.9) {
    outValue.innerHTML = "The wind turbine produces about 60 watts per hour";
  } else if (strengthselected == 8 - 10.7) {
    outValue.innerHTML = "The wind turbine produces about 150 watts per hour";
  } else if (strengthselected == 10.8 - 13.8) {
    outValue.innerHTML = "The wind turbine produces about 400 watts per hour";
  } else if (strengthselected == 13.4 - 17.1) {
    outValue.innerHTML = "The wind turbine produces about 50 watts per hour";
  } else {
    outValue.innerHTML = "The wind turbine produces 0 watts per hour";
  }

}
<body>
  <input type="number" id="wind-strength" placeholder="Enter wind strength in m/s">

  <button onclick="myFunction()"> Press </button>

  <p id="out-value"> </p>
</body>
Shariq Shahid
  • 272
  • 12