JSFiddle - React, Tailwind, and code Playground
by InferOn
HTML
<p>"oh no theres a monster whatchya gonna do?"</p>
<input id="plyrInput" type="text" />
<button onclick="analyzeUserInput()">Try it</button>
<p id="text"></p>
JavaScript
var plyrInput, plyrInputArray, plyrInputAnalysis;
var commands = ['use', 'slash', 'hack', 'wield', 'slice', 'sever', 'dismember', 'poke', 'cripple', 'maim', 'mutilate', 'chop', 'rend'];
function oc() {
plyrInputArray = plyrInput.split(' ');
var plyrInputObj = {};
for (var i = 0; i < plyrInputArray.length; ++i) {
plyrInputObj[plyrInputArray[i]] = ' ';
}
return plyrInputObj;
}
function analyzeUserInput() {
plyrInput = document.getElementById("plyrInput").value;
oc();
for (var i = 0; i < plyrInputArray.length; i++) {
var currentArg = plyrInputArray[i];
for (var i = 0; i < commands.length; i++) {
if (currentArg == 'sword') {
document.getElementById("text").innerHTML = "You did 4 damage with your sword!";
break;
}
}
}
//for (var i = 0; i < plyrInput.length; i++) {
// var currentArg = plyrInput[i];
// for (var i = 0; i < commands.length; i++) {
// if (currentArg == 'sword') {
// document.getElementById("text").innerHTML = "You did 4 damage with your sword!";
// break;
// }
// }
//}
//oc();
//if (plyrInputAnalysis in oc(commands) && plyrInputAnalysis in oc(['sword'])) {
// document.getElementById("text").innerHTML = "You did 4 damage with your sword!";
//}
}