Archive:Debugging challenges: Difference between revisions
wikademia>BotheredByBees |
|||
| Line 1: | Line 1: | ||
These debugging challenges are written in JavaScript, and designed to complement the units: [[ICAB4225A Automate processes]] and [[ICAT4221A Locate equipment, system and software faults]] | These debugging challenges are written in JavaScript, and designed to complement the units: [[ICAB4225A Automate processes]] and [[ICAT4221A Locate equipment, system and software faults]] | ||
== Challenge 1 - Lexical Errors == | == Challenge Set 1 - Lexical Errors == | ||
Lexical errors are introduced when the programmer enters symbols that are not part of the programming language. Unlike when you misspell a word in English, this inevitably leads to a breakdown in programming communications. See if you can see (or find) the lexical errors in the following code snippets... | Lexical errors are introduced when the programmer enters symbols that are not part of the programming language. Unlike when you misspell a word in English, this inevitably leads to a breakdown in programming communications. See if you can see (or find) the lexical errors in the following code snippets... | ||
| Line 9: | Line 9: | ||
alert("the product of " + num1 + " and " + num2 + " is " + product); | alert("the product of " + num1 + " and " + num2 + " is " + product); | ||
</script> | </script> | ||
<script type="text/javascript"> | |||
/* This script and many more are available free online at | |||
The JavaScript Source!! http://javascript.internet.com | |||
Created by: Patrick Fitzgerald | http://www.barelyfitz.com/ */ | |||
function truncate() { | |||
var len = 100; | |||
var p = document.getElementByID('truncateMe'); | |||
if (p) { | |||
var trunc = p.innerHTML; | |||
if (trunc.length > len) { | |||
/* Truncate the content of the P, then go back to the end of the | |||
previous word to ensure that we don't truncate in the middle of | |||
a word */ | |||
trunc = trunc.substring(0, len); | |||
trunc = trunc.replace(/\w+$/, ''); | |||
/* Add an ellipses to the end and make it a link that expands | |||
the paragraph back to its original size */ | |||
trunc += '<a href="#" ' + | |||
'onclick="this.parentNode.innerHTML=' + | |||
'unescape(\''+escape(p.innerHTML)+'\');return false;">' + | |||
'<span style="font-size: 10px;">[ more ... ]</span><\/a>'; | |||
p.innerHTML = trunc; | |||
} | |||
} | |||
} | |||
// Multiple onload function created by: Simon Willison | |||
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent | |||
function addLoadEvent(func) { | |||
var oldonload = window.onload; | |||
if (typeof window.onload != 'function') { | |||
window.onload = func; | |||
} else { | |||
window.onload = function() { | |||
if (oldonload) { | |||
oldonload(); | |||
} | |||
func(); | |||
} | |||
} | |||
} | |||
addLoadEvent(function() { | |||
truncate(); | |||
}); | |||
</script> | |||
<p id="truncateMe">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean consectetuer. Etiam venenatis. Sed ultricies, pede sit amet aliquet lobortis</p> | |||
== Challenge 2 - Syntactic Errors == | == Challenge 2 - Syntactic Errors == | ||
Revision as of 01:59, 21 May 2007
These debugging challenges are written in JavaScript, and designed to complement the units: ICAB4225A Automate processes and ICAT4221A Locate equipment, system and software faults
Challenge Set 1 - Lexical Errors
Lexical errors are introduced when the programmer enters symbols that are not part of the programming language. Unlike when you misspell a word in English, this inevitably leads to a breakdown in programming communications. See if you can see (or find) the lexical errors in the following code snippets...
<script type="text/javascript">
var num1 = 2;
var num2 = 3;
var product = num1 x num2;
alert("the product of " + num1 + " and " + num2 + " is " + product);
</script>
<script type="text/javascript"> /* This script and many more are available free online at The JavaScript Source!! http://javascript.internet.com Created by: Patrick Fitzgerald | http://www.barelyfitz.com/ */ function truncate() { var len = 100; var p = document.getElementByID('truncateMe'); if (p) { var trunc = p.innerHTML; if (trunc.length > len) {
/* Truncate the content of the P, then go back to the end of the previous word to ensure that we don't truncate in the middle of a word */ trunc = trunc.substring(0, len); trunc = trunc.replace(/\w+$/, );
/* Add an ellipses to the end and make it a link that expands the paragraph back to its original size */ trunc += '<a href="#" ' + 'onclick="this.parentNode.innerHTML=' + 'unescape(\+escape(p.innerHTML)+'\');return false;">' + '[ more ... ]<\/a>'; p.innerHTML = trunc; } } }
// Multiple onload function created by: Simon Willison // http://simon.incutio.com/archive/2004/05/26/addLoadEvent function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } }
addLoadEvent(function() { truncate(); }); </script>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean consectetuer. Etiam venenatis. Sed ultricies, pede sit amet aliquet lobortis
Challenge 2 - Syntactic Errors
A syntax error occurs when you use the right symbols, but in a way that doesn't make sense to the compiler or interpreter. Something like the English sentence: "You don't program good"
<script type="text/javascript">
var x;
x=prompt("What is 5 x 5?");
while (x != 25) do
{
alert("Wrong answer");
x=prompt("What is 5 x 5?",0);
}
document.write("Right, the answer is " + x)
</script>
Challenge 3 - Execution Errors
Execution errors are impossible requests made in perfectly correct programming terms.
<script type="text/javascript">
var weekdays=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
for (i=1; i<8; ++i)
{
document.write(weekdays[i] + "
");
}
</script>
Challenge 4 - Logic Errors
These are errors introduced Through faulty design. Once again, the syntax is correct, but the results are unexpected
TODO - add more examples of each