Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Im learning JS and i have a task to make the numbers that i input reverse and pop up in an alert

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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>
about 4 years ago · Juan Pablo Isaza Report

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>
about 4 years ago · Juan Pablo Isaza Report

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!