Practice Set: Parameter Validation
for CSCI E3, Harvard University author(s): Larry Bouthillier
by DustyWhite
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
// NOTE: All of (my) comments are preceeded with:
// *** DS:
// *** DS: = "Dusty says" -- I think this is the easiest way to skim through to see MY THOUGHTS below.
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
var scoresArray4 = "this is not an array";
console.log("scoresArray4 is a " + typeof scoresArray4); // *** DS: CONSOLE LOG SAYS: "scoresArray4 is a string" This makes sense (to me) as the "string" is not enclosed in square brrackets (" [ ] "), and also there are no commas. To be an array, it should have both. So it is truthy, but not an array -- therefore it is a primitive and not an object. That much I get. Why that is important remains a mystery.
var scoresArray5 = [76, 83, "90"];
// *** DS: Okay, this is at the far reaches of my awareness, but I can break the problem down into tiny steps. First we have to grab the STRING "90" from the array. I will use the SLICE keyword (javasript thing) to grab just that, so I can mangle it:
var slice90 = scoresArray5.slice(2);
// *** DS: Now that I have it represented by a variable, I can turn it into a true and correct NUMBER:
var thirdElement = parseInt(slice90);
// *** DS: Now I am testing my hypothesis (before I go too far down any rabbit holes -- after all, I have read THAT book):
console.log(thirdElement);
// *** DS: So far, so good. Now I need to cut out "90" and replace it with 90. There is almost certainly a much more "elegant" way of doing this, but you might as well ask me to write it in Mandarin (I know neither).
function deathToStrings() {
scoresArray5.pop("90");
scoresArray5.push(90);
}
// *** DS: Now to test this monstrosity:
deathToStrings();
console.log("scoresArray5 is now " + "[ " + scoresArray5 + " ]")
// *** DS: Hmm . . . Maybe I should have just run a function to begin with. Anyway, the altered array now should TYPEOF as a number:
console.log("scoresArray5 is now an " + typeof scoresArray5);
// *** DS: Yeah, I am surprised that worked too!
var scoresArray6 =...