1. Ten Simple JavaScript Exercises
1 of 10 simple JavaScript exercises.
http://www.ling.gu.se/~lager/kurser/webtechnology/lab4.html
JavaScript Exercise:
Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript.
HTML
<div id="console-log"></div>
JavaScript
//
// Public domain
// This whole code come from Douglas Crockford's JSON2 library except some
// minor change on the first line of the str() function (see next comment)
//
//
var STRINGYFY = function() {
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent,
meta = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
rep;
function quote(string) {
escapable.lastIndex = 0;
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string'
? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' : '"' + string + '"';
}
function str(key, holder) {
var i,
k,
v,
length,
mind = gap,
partial,
value;
//
// Changes from Douglas Crockford's version, the five next code lines
// avoid errors when accessing a value through a getter that raise an
// ulgy error
// previously : value = holder[key];
//
try {
value = holder[key];
} catch (Error) {
value = "accessor error";
}
if (value && typeof value === 'object' &&
typeof value.toJSON === 'function') {
value = value.toJSON(key);
}
if (typeof rep === 'function') {
value = rep.call(holder, key, value);
}
switch (typeof value) {
case...