Refactoring a function to return all scores above a certain threshold I am currently learning JavaScript and wanted to fellow coders to review a function that I had written. I am looking for feedback particularly on:


Whether I have followed best practice and JavaScript conventions
Any suggestion on doing it better.


Things I know I need to implement but don't know how:


Validate if array values are numbers


Purpose of function: Return all scores above a certain threshold. Function accepts two arguments. First is an array of scores and second argument is the threshold.

My code:

UPDATED code after putting it through JS lint - I have ignored some erros like moving vars in the loops to the top or not declaring vars in the for loop etc (before JSLint code is here http://jsbin.com/UtOMAyU/1/)

function scoresAbove(scoresArg, thresholdArg) {
    "use strict";
    var output = [],
        nums = scoresArg,
        threshold = thresholdArg;
    //check if scores Arg provided is an array
    if (typeof nums !== "object" || !nums.hasOwnProperty('length')) {
        return "Error: Number(s) not in the expected format. Please provid number(s) in an array format";
    }else if (typeof threshold !== "number") { //check if threshold Arg provided is an array
        return "Error: Threshold number not in the expected format. Please provid a threshold";
    }
    for (var i = 0, len = nums.length; i &lt; len; i++) {
        var currentNum = nums[i];
        if (currentNum &gt; threshold) {
            output.push(currentNum);
        }
    }
    //What to return from func
    if (output == "") {
        return "No scores were above the " + " threshold you set as " + threshold;
    }
    return "The following scores were above the threshold: " + output.join(", ");
}
My 2 cents:


var output should probably be highScores or some such, since it never contains what is returned, nor does it get outputted
nums = scoresArg, threshold = thresholdArg; makes little sense, just use the arguments
function scoresAbove(scoresArg, thresholdArg) , I am not sure what the value is of postpending 'Arg' to the argument names, I would go for simply function scoresAbove(scores, threshold)
The check for Array should really be a separate function, it is completely re-usable
Ditto for Number
For the actual filtering , I would use the built-in filter : 

highScores = scores.filter( function(v){ return v &gt; threshold });
Since output is an array, I would not compare with ""
Why the double concatenation here: return "No scores were above the " + " threshold you set as " + threshold;
Arguments of the wrong type should be rare enough that you can throw an exception since something is clearly wrong with the program.


You could try

function scoresAbove( scores, threshold ) 
{
  "use strict";
  var highScores;

  if( notArray( scores ) )
    throw "scores is not an array";
  if( notNumber( treshold ) )
    throw "treshold is not a number"

  highScores = scores.filter( function(v){ return v &gt; threshold });

  if( !highScores || !highScores.length )
    return "No scores were above the threshold you set as " + threshold;

  return "The following scores were above the threshold: " + output.join(", ");
}


You would have to write the notNumber and notArray function of course.
JSLint migh complain about missing curly braces, to each his own ;)