Skip to content

Instantly share code, notes, and snippets.

@theMagicalKarp
Created April 4, 2014 02:49
Show Gist options
  • Save theMagicalKarp/9967156 to your computer and use it in GitHub Desktop.
Save theMagicalKarp/9967156 to your computer and use it in GitHub Desktop.
The goal of this assignment is to familiarize you with array handling in javascript. This is important because handling lists is very common when rendering html on a page. As a guideline you should only have to modify code inside the provided functions with comments. The rest of the code tests your implementations gives you feedback on your prog…
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assignment 1</title>
<style>
body {
text-align:center;
}
p, h1 {
font-family: courier;
}
p.pass {
color: green;
}
p.fail {
color: red;
}
</style>
</head>
<body>
<h1>Test Results</h1>
<div id="test-results" >
</div>
<script type="text/javascript">
(function() {
var a = function(items) {
// find all the elements that are divisible by 2 and return them in a array
return [];
}([99, 21, 11, 84, 75, 98, 65, 14, 47, 1]);
var b = function(items) {
// square each element of the array and return the array.
return [];
}([1, 18, 11, 53, 39, 63, 21, 89, 36, 61]);
var c = function(items) {
// concat each element into a stirng delimited by hyphens then return the string
// for example [1, 2, 3] would be transformed into '1-2-3'
return '';
}([23, 80, 92, 32, 43, 10, 40, 38, 97, 31]);
var d = function(items) {
// Turn the string of comma delimited numbers into an array of numbers as strings
// for example '1, 2, 3' would be transformed into ['1', '2', '3']
return [];
}('3, 6, 12, 42, 1, 5, 11, 19, 18, 16');
var e = function(items) {
// Sort the list in acsending order and return it
return [];
}([51, 68, 13, 76, 85, 74, 52, 12, 28, 10]);
var f = function(items) {
// Sort the list in descending order and return it
return [];
}([57, 9, 40, 12, 43, 58, 28, 45, 85, 33]);
var g = function(items) {
// find every element in the array that's divisible by 2
// sort those elements in ascending order
// then multiply each of those elements by 5 and subtract 10
// then concat those elements into a stirng delimited by commas!
return '';
}([87, 78, 32, 97, 43, 33, 51, 6, 42, 30]);
var correctAnswers = {
'a': [84, 98, 14],
'b': [1, 324, 121, 2809, 1521, 3969, 441, 7921, 1296, 3721],
'c': '23-80-92-32-43-10-40-38-97-31',
'd': ['3', '6', '12', '42', '1', '5', '11', '19', '18', '16'],
'e': [10, 12, 13, 28, 51, 52, 68, 74, 76, 85],
'f': [85, 58, 57, 45, 43, 40, 33, 28, 12, 9],
'g': '20, 140, 150, 200, 380'
};
var yourAnswers = {
'a': a,
'b': b,
'c': c,
'd': d,
'e': e,
'f': f,
'g': g,
};
var results = ['a', 'b' , 'c', 'd', 'e', 'f', 'g'].map(function(key) {
if (JSON.stringify(correctAnswers[key]) === JSON.stringify(yourAnswers[key])){
return ['<p class="pass">', key, ': pass!</p>'].join('');
} else {
return [
'<p class="fail">', key, ': fail...</p>',
'<p>Correct Answer: ', correctAnswers[key], '</p>',
'<p>Your Answer:&nbsp;&nbsp; ', yourAnswers[key],'</p>'
].join('');
}
}).join('<br>');
document.getElementById('test-results').innerHTML = results;
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment