Practice Set: Parameter Validation

by Thomas Smalls

HTML

<p>There is one part to this practice set. This exercise is designed to give you practice validating input, in this case, arguments to a function.   </p>
<p>
Our function, calculateSum(), adds the numbers in the array and returns the sum.  We've created three "arrays" of input data to for our function, each of which will fail in some way if your function doesn't include code to deal with input of the 'wrong' type. </p>
<p>
A better solution would be to check for the kind of error condition that each  example contains.  To do this, you'll want to tackle the cases presented in the sample arrays one at a time.  You will modify the calculateSum() function to handle each of these cases.  With each change, your code will become more robust (specifically, more tolerant of unexpected input).  See the comments in the code for some suggestions. 
</p>
The output will appear here:
<ul>
<li>Sum 4: <span id="sum4"></span></li>
<li>Sum 5: <span id="sum5"></span></li>
<li>Sum 6: <span id="sum6"></span></li>
</ul>
<p>
Finally, one test we didn't do was for the case where nothing is passed in as an argument.  If you can, modify the code you wrote for the scoresArray4 case to also hande the case in which the input is undefined. 
</p>

JavaScript

// the sample input data we'll be validating
console.clear();
var scoresArray4 = "this isn't an array";  
//hint: try testing to see if this is actually an array before doing anything else in the function.  Remember that 'typeof' works for simple data types, but [object].constructor.name will tell you the type of many objects. We show this in video 4.4 (towards the end).   

var scoresArray5 = [76, 83, "90"];
// hint: try converting each (or each non-numeric) array element to a number as you come to it

var scoresArray6 = [88, 73, "bob", 100]; 
// hint: while you're converting all array elements to numbers, use a conditional to check to see if it worked before adding the value to the sum

// function calculateSum() is here
// You will modify this function so that none of the above cases
// "break" the code. If the sum simply can't be done (as in scoresArray4),
// return the value 'null' or nothing at all, and write an error to the console.
// Otherwise, if a sum can still be calculated, return that
function calculateSum(arr){
  console.log(arr);
  console.log(arr.length);
  var tellMe = Array.isArray(arr);
  console.log(tellMe);
  console.log('\n');

  var sum=0;
  
  //Only actual arrays will be calculated
  if(Array.isArray(arr)){
  
    for (var i=0; i<arr.length; i++){
    
      //Checking each array element to make sure it is a number
      if(typeof (arr[i])==='number'){ 
      	sum+=arr[i];
      	console.log(sum);
        
      //Parse any number that appears as a string
      } else if(typeof (arr[i])==='string') {
      
      	//Assign local variable to separate NaNs from other number values
        let a = parseInt(arr[i]);
        
          //If element can't be parsed as a number, skip it (e.g., "bob")
    			if(isNaN(a)){
    				sum+=0;
          
          //If it can, add to sum; this will add the 90 in ScoresArray5
      		} else {
          	sum+=a;
          }
      	}
        
      } 
      	//Pass sum onto to the HTML div 
  			return...