1

hello im trying to get the minumum value of this array am i adding the user input to the array propperly.

var highestMark=0;
var gradeAwarded;
var StudentArr= [Student];
var markArr = [mark];
var Student = prompt("Enter Student Name: ", "Ross");
var mark = prompt("Enter Student Mark: ", 50);
var max;

function min (mark){
    var min = Number.Max_Value;
    for(var i = 0; i < mark.length; i++)
    if(mark[i] < min)
        min = mark[i];
    }
    return mark;

var smallest = min (mark);
document.write(smallest);
1
  • The typical JS way: function min(xs){return Math.min.apply(0,xs)} Commented Mar 30, 2014 at 22:54

1 Answer 1

1

Ok, it's possible, but the prompt method return a string and you can lead with this string the way you want. For sample, if you user type some values separated by a specific char , (for sample) you could use the split method and get this array, for sample:

var values = prompt("Eter values: ", "");
var result = values.split(',');

but your code looks fine, just convert the mark[i] to number, to sample:

function min (mark){
    var min = Number.Max_Value;
    for(var i = 0; i < mark.length; i++)
       if(number(mark[i]) < min)
           min = number(mark[i]); 
    return mark;
}
Sign up to request clarification or add additional context in comments.

1 Comment

do you mean like that?

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.