1

I'm really new to this, no errors are showing up, but code doesn't work. Trying to get the min and max value of numbers entered in the text-box.

here's the code:

<html lang="en">

<body>
  <h1>Problem JavaScript</h1>
  Enter a series of numbers with spaces in between each:
  <input type="text" name="qty1" id="qty"/>
  <button type="button" onclick="calculate();">Enter</button>
</body>
</html

function calculate(){

var numInput = getElementById("qty");
var numArray = numInput.split(" ");

document.write('Min value is:', Math.min.apply(null, numArray));
document.write('Max value is:', Math.max.apply(null, numArray));
}
2
  • 4
    Are you sure you posted your full HTML? Commented Jan 1, 2016 at 0:47
  • Paste your full HTML code. I see a HTML tag without closing Commented Jan 1, 2016 at 1:09

1 Answer 1

2

You almost got it.

1.) getElementById should be document.getElementById. See document.getElementById() VS. getElementById()

2.) Calling .split on the element returned by getElementById isn't going to work, you need to do that to the element's value. ie:

var numInput = document.getElementById("qty").value;
var numArray = numInput.split(" ");

Here's a full example

function calculate() {
  var numInput = document.getElementById("qty").value;
  var numArray = numInput.split(" ");

  document.write('Min value is:', Math.min.apply(null, numArray));
  document.write('Max value is:', Math.max.apply(null, numArray));
}
<h1> Problem JavaScript</h1> Enter a series of numbers with spaces in between each:
<input type="text" id="qty">
<input type="button" id="go" value="Go" onclick="calculate()">

Sign up to request clarification or add additional context in comments.

5 Comments

Face palm moment with the document.getElementById...Thanks so much! been staring at it for a while and couldn't figure it out....
Of course! Consider marking this answer as correct to help people find it in the future
to get the sum..i tried math.sum but that didn't work..will i need to use the .reduce?
.reduce will work, but as it stands, your array is actually an array of strings, not integers. So, you'll need to map it to integers first. Check out this fiddle: jsfiddle.net/q9d6urkr
Thank you so much for the help..i looked at .map and .reduce but couldn't figure out how to write it to work

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.