<!doctype html>
<html>
<head>
<title>Javascript instanceof Usage - RJM Programming - March, 2018</title>
<style>
body, #mydiv { background-color: yellow; }
</style>
</head>
<body style='background-color:yellow;'>
<h1 style='width:100%;height:40px;' id='myh1'></h1>
<div style='width:100%;height:98vh;' id='mydiv'></div>
<script type='text/javascript'>

var psc="<scr" + "ipt type='text/javascript'> function xxx() { } var zzz = new xxx(); </sc" + "ript>";
var altpsc=" function xxx() { } var zzz = new xxx(); ";
var parts=[];
var delimiter='"';
var lastdelimiter='"';

function delimit(what) {
var thisd='"';
if (what.indexOf('?') == 0) {
return lastdelimiter + what + lastdelimiter;
} else if (what != '') {
thisd='';
for (var iu=0; iu<what.length; iu++) {
if (what.substring(iu,eval(1 + iu)) == '-') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '.') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '0') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '1') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '2') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '3') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '4') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '5') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '6') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '7') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '8') {
thisd=thisd;
} else if (what.substring(iu,eval(1 + iu)) == '9') {
thisd=thisd;
} else {
thisd='"';
}
lastdelimiter=thisd;
delimiter=thisd;
}
} else {
lastdelimiter=thisd;
delimiter=thisd;
}
return thisd + what + thisd;
}

function altit(what) {
var rewhat="";
for (var iii=0; iii<what.length; iii++) {
if (eval(iii% 2) == 0) {
rewhat+='?';
} else {
rewhat+=what.substring(iii,eval(1 + iii));
}
}
return rewhat;
}

function fixc() {
document.body.style.backgroundColor='yellow';
document.getElementById('myh1').innerHTML='Javascript instanceof Usage';
document.getElementById('mydiv').innerHTML='<br><br>RJM Programming - March, 2018<br><br>Thanks to <a target=_blank title="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof</a>';
}

function newgo() {
location.href=document.URL;
}

function analyze(results) {
if (results) {
parts=results.split(';');
if (parts.length >= 3) {
if (parts[0] != "" && parts[1] != "" && parts[2].replace(/\ /g,'').replace(/\,/g,'') != "") {
var features=parts[2].trim().replace(/\ /g,'_').split(',');
var altfeatures=parts[2].trim().replace(/\ /g,'_').split(',');
if (parts.length >= 4) altfeatures=parts[3].trim().replace(/\ /g,'_').split(',');
psc=psc.replace(/ion xxx\(/g, 'ion ' + parts[1].trim().replace(/\ /g,'_') + '(' + features[0].trim().replace(/\ /g,'_'));
psc=psc.replace(/\{/g, '{ this.' + features[0].trim().replace(/\ /g,'_') + ' = ' + features[0].trim().replace(/\ /g,'_') + '; ');
altpsc=altpsc.replace(/ion xxx\(/g, 'ion ' + altit(parts[1].trim().replace(/\ /g,'_')) + '(' + altit(features[0].trim().replace(/\ /g,'_')));
altpsc=altpsc.replace(/\{/g, '{ this.' + altit(features[0].trim().replace(/\ /g,'_')) + ' = ' + altit(features[0].trim().replace(/\ /g,'_')) + '; ');
if (parts.length >= 4) {
psc=psc.replace(/new xxx\(/g, 'new ' + parts[1].trim().replace(/\ /g,'_') + '(' + delimit(altfeatures[0].trim().replace(/\ /g,'_')) + '');
altpsc=altpsc.replace(/new xxx\(/g, 'new ' + altit(parts[1].trim().replace(/\ /g,'_')) + '(' + delimit(altit(altfeatures[0].trim().replace(/\ /g,'_'))) + '');
} else {
psc=psc.replace(/new xxx\(/g, 'new ' + parts[1].trim().replace(/\ /g,'_') + '(""');
altpsc=altpsc.replace(/new xxx\(/g, 'new ' + altit(parts[1].trim().replace(/\ /g,'_')) + '(""');
}
psc=psc.replace(/var zzz/g, 'var ' + parts[0].trim().replace(/\ /g,'_'));
altpsc=altpsc.replace(/var zzz/g, 'var ' + altit(parts[0].trim().replace(/\ /g,'_')));
for (var ii=1; ii<features.length; ii++) {
psc=psc.replace(/\) \{/g, ',' + features[ii].trim().replace(/\ /g,'_') + ') {');
psc=psc.replace(/\}/g, ' this.' + features[ii].trim().replace(/\ /g,'_') + ' = ' + features[ii].trim().replace(/\ /g,'_') + '; }');

altpsc=altpsc.replace(/\) \{/g, ',' + altit(features[ii].trim().replace(/\ /g,'_')) + ') {');
altpsc=altpsc.replace(/\}/g, ' this.' + altit(features[ii].trim().replace(/\ /g,'_')) + ' = ' + altit(features[ii].trim().replace(/\ /g,'_')) + '; }');

if (parts.length >= 4) {
psc=psc.replace(/\)\;/g, ',' + delimit(altfeatures[ii].trim().replace(/\ /g,'_')) + ');');
altpsc=altpsc.replace(/\)\;/g, ',' + delimit(altit(altfeatures[ii].trim().replace(/\ /g,'_'))) + ');');
} else {
psc=psc.replace(/\)\;/g, ',"");');
altpsc=altpsc.replace(/\)\;/g, ',"");');
}
}
//for (var jj=4; jj<parts.length; jj++) {
//}
return psc;
} else {
return "<a href='" + document.URL + "' title='Try again' target=_self>Try again</a>";
}
} else {
return "<a href='" + document.URL + "' title='Try again' target=_self>Try again</a>";
}
} else {
return "<a href='" + document.URL + "' title='Try again' target=_self>Try again</a>";
}
}

setTimeout(fixc, 1000);

document.write(analyze(prompt("Get your playing partner to turn away now. " + String.fromCharCode(10) + String.fromCharCode(10) + " Okay then, tell me what you want to define, and your playing partner will try to guess from incomplete details, in terms of " + String.fromCharCode(10) + String.fromCharCode(10) + "instanceof name;Class name;feature1,feature2,feature3 (etcetera)[;optionalvalue1,optionalvalue2,optionalvalue3 (etcetera)] " + String.fromCharCode(10) + String.fromCharCode(10) + "eg." + String.fromCharCode(10) + String.fromCharCode(10) + "auto;Car;make,model,year;Honda,Accord,1998", ";;,")));

//function Car(make, model, year) {
// this.make = make;
// this.model = model;
// this.year = year;
//}
//var auto = new Car('Honda', 'Accord', 1998);

var ans=prompt("Okay second guessing player, change the ? to reflect how your playing partner thinks", altpsc);

if (ans) {
if (psc.indexOf(ans) != -1 && ans.indexOf(';') != -1) {
alert('Very good. You are correct. Which means ' + String.fromCharCode(10) + String.fromCharCode(10) + '(' + parts[0] + ' instanceof ' + parts[1] + ') = ' + eval(parts[0] + ' instanceof ' + parts[1]) + String.fromCharCode(10) + '(' + parts[0] + ' instanceof Object) = ' + eval(parts[0] + ' instanceof Object'));
setTimeout(newgo, 2000);
} else {
alert('Bad luck. You needed to put Javascript like ' + String.fromCharCode(10) + String.fromCharCode(10) + psc.substring(psc.indexOf(' function')).replace('</sc' + 'ript>','').replace('}','}' + String.fromCharCode(10)).replace('{','{' + String.fromCharCode(10)).replace(/\;/g,';' + String.fromCharCode(10)) + String.fromCharCode(10) + String.fromCharCode(10) + ' // Which means ' + String.fromCharCode(10) + '(' + parts[0] + ' instanceof ' + parts[1] + ') = ' + eval(parts[0] + ' instanceof ' + parts[1]) + String.fromCharCode(10) + '(' + parts[0] + ' instanceof Object) = ' + eval(parts[0] + ' instanceof Object'));
setTimeout(newgo, 2000);
}
} else {
alert('You needed to put Javascript like ' + String.fromCharCode(10) + String.fromCharCode(10) + psc.substring(psc.indexOf(' function')).replace('</sc' + 'ript>','').replace('}','}' + String.fromCharCode(10)).replace('{','{' + String.fromCharCode(10)).replace(/\;/g,';' + String.fromCharCode(10)) + String.fromCharCode(10) + String.fromCharCode(10) + ' // Which means ' + String.fromCharCode(10) + '(' + parts[0] + ' instanceof ' + parts[1] + ') = ' + eval(parts[0] + ' instanceof ' + parts[1]) + String.fromCharCode(10) + '(' + parts[0] + ' instanceof Object) = ' + eval(parts[0] + ' instanceof Object'));
setTimeout(newgo, 2000);
}

//try {
//alert(auto instanceof Car);
//} catch (eee) {
//alert('false');
//}

//try {
//alert(auto instanceof Object);
//} catch (eee) {
//alert('false');
//}

//location.href=document.URL;

</script>
</body>
</html>