-1

I'm trying to make the short Javascript code throw an alert and show 93 instead of 0-93 but it's not working ?

<html>
    <head>
    <script type="text/javascript">
    function numberFromInput(value) {
  return  alert(Number(value.match(/[-]?(\d*)$/).pop()));
}
</script>
</head>
<body>

numberFromInput(0-93);

</body>
</html>
4
  • 4
    You're not actually calling your function. If you want to call it, you need to do that within a <script> tag as well. Otherwise, your numberFromInput(0-93); is just a text node on the page. Commented Jan 31, 2013 at 21:55
  • I recommend to read w3.org/wiki/Your_first_look_at_JavaScript. Commented Jan 31, 2013 at 22:00
  • that still doesn't throw the alert Commented Jan 31, 2013 at 22:00
  • You're trying to perform value.match, but the value you pass is a Number (0 - 93 will evaluate to -93) Commented Jan 31, 2013 at 22:10

1 Answer 1

3

You have to call the function (you're just displaying its call code as content). And you have to pass the value as a string (you need quotes around 0-93):

<script type="text/javascript">
function numberFromInput(value) {
    return alert(Number(value.match(/[-]?(\d*)$/).pop()));
}
numberFromInput("0-93");
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

It works for me: jsbin.com/izalux/1/edit. Are you sure you did include the quotes around the string when you first tried it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.