0

i made the script that reverses the numbers but i dont know how to make the alert pop up the result of the reversed numbers I need help to figure this out it probably has a simple solution but i dont know

The code added to snippet is below:

function okreni () { // removed "s" parameter
  var a = ' ';
  // s = s.toString();
  const s = document.getElementById("broj").value.toString();
  for (var i = s.length - 1; i>=0; i--) {
    a += s[i];
  }
  window.alert (a);
};
<body>
  <label for="broj">Unesite Broj:</label>
  <input type="number" name="broj" id="broj" value="">
  <div>
    <button  value="okreni" onclick="okreni()">Okreni</button>
  </div>
</body>

EDIT -

The s = s.toString() has been changed to get the information from the input-value.

jsN00b
  • 3,584
  • 2
  • 8
  • 21
Capser
  • 3
  • 2
  • Since you are not passing anything to the `okreni()` function `s` doesn't hold a value. You need to add some code to grab the value from the `` element inside of `okreni` and store that in `s`. You can see [this answer](https://stackoverflow.com/a/11563667) on how to get the value from your number input. – Nick Parsons Apr 09 '22 at 10:23
  • An edit has been submitted for the question. That should explain the issue (basically with `s = s.toString()`). The parameter gets nothing. Instead, using `document.getElementById("broj").value.toString()` resolve the issue faced. – jsN00b Apr 09 '22 at 10:31

3 Answers3

0

alert doesn't display if there's no value to display. in your case you have to passe a value to "okreni()" function.

<button  value="okreni" onclick="okreni(**value**)">Okreni</button>
0

Apparently, you suppose to get the input value as s in okreni(s). However, this is not possible. You have to get the value programatically from the input. Following the working code. I've also created this CodeSandbox for you to try it out:

<!DOCTYPE html>
<html>
  <head>`enter code here`
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>
  <body>
    <label for="broj">Unesite Broj:</label>
    <input type="number" name="broj" id="broj" value="" />
    <div>
      <button value="okreni" onclick="okreni()">Okreni</button>
    </div>
    <script type="text/javascript">
      function okreni() {
        var a = " ";
        let inputValue = document.querySelector("#broj").value;
        const s = inputValue.toString();
        for (var i = s.length - 1; i >= 0; i--) {
          a += s[i];
        }
        window.alert(a);
      }
    </script>
  </body>
</html>
alexanderdavide
  • 1,487
  • 3
  • 14
  • 22
0

You could also try something like this to reverse your string. In looks much cleaner in my opinion and can even be condensed to a single line if needed.

Apart from that, the reason you are getting an error is because of what alexanderdavide mentioned in his answer. To elaborate further, the okreni function does not require a parameter to be passed. Instead, within the fucntion we look for the value in the input element with the id of broj. So, when you click on the button, the function checks the string in that input, reverses it and then performs an alert.

function okreni() {
  let s = document.getElementById('broj').value
  s = s.split("").reverse().join("")
  window.alert(s)
}
<label for="broj">Unesite Broj:</label>
<input type="text" name="broj" id="broj" value="">
<div>
  <button value="okreni" onclick="okreni()">Okreni</button>
</div>
AmehPls
  • 158
  • 1
  • 12