The goal is to write program that will produce a word unexpected
I didn't read past that. I have a serious issue with reading long passages (and error messages).
I decided to create a simple program that alerts "5". Unfortunately, I can't seem to get it to work.
(function () {
"use strict";
function logError(e) {
// I have a serious issue with reading long error messages
// I'll just print the first word of the error and figure out what it means
console.log(e.message.split(" ")[0]);
}
// Useful assert method for debugging
function assert(value, message) {
if (value === false) {
throw new Error(message);
}
}
// Sets a varaible "a" to 5 and alerts it
try {
// Try it the old fashioned way
a = 5;
alert(a);
} catch (e) {
logError(e);
// In some legacy browsers, that might now work
// because alert requires a string
try {
// create objA which has a method "word", which always returns a word, or a string
var objA = {
word: function () {
return new String(5);
}
};
// Make sure it is a string
assert(typeof objA.word() === "string", "word didn't return a string");
alert(objA.word());
} catch (e) {
logError(e);
// Some browsers, such as chrome, just won't work
// It's time to be evil and force them to work!
try {
eval("a = 5" +
"alert(a)");
} catch (e) {
logError(e);
}
}
}
})();
Tested in the google chrome console.
It produces (literally) a word unexpected.
http://jsfiddle.net/prankol57/Af4sH/
(For the jsfiddle, you must open your console, there won't be any html output)