JSFiddle - React, Tailwind, and code Playground
by Jason Tame
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<body>
<div class = "info">
<h1>
The numerical symmetry of a word
</h1>
<p>
This program was created to fiddle around with Javascript. It assigns an integer to each letter in a string and places these integers in a new array. The number assigned corresponds to the amount of key presses required to select that number on mobile phone keypads. The program then tests the new array to see if the integer combinations are symmetrical.
</p>
</div>
<form method = "POST" name = "myForm" class = "wordInput">
<input type = "text" name = "wordInput" required>
<input type = "button" name = "Submit" value = "Click" onClick = "getWord(this.form)">
</form>
</body>
CSS
.info {
text-align: center;
}
.wordInput {
text-align: center;
}
JavaScript
function numValue(char) {
switch(char) {
case "A":
case "D":
case "G":
case "J":
case "M":
case "P":
case "T":
case "W":
return 1;
break;
case "B":
case "E":
case "H":
case "K":
case "N":
case "Q":
case "U":
case "X":
return 2;
break;
case "C":
case "F":
case "I":
case "L":
case "O":
case "R":
case "V":
case "Y":
return 3;
break;
case "S":
case "Z":
return 4;
break;
}
}
function stringToIntArray(str) {
intArray = [];
string = str.toUpperCase();
length = string.length;
for(i = 0; i < length; i++) {
intArray.push(numValue(string.charAt(i)));
}
return intArray;
}
function isSymmetrical(numberArray) {
var j = 0;
var symmetry = true;
var length = numberArray.length;
for (i = length - 1; i < length/2; i--)
if (numberArray[i] !== numberArray[j] ) {
symmetry = false;
break;
}
j++;
}
if (symmetry) {
return "The word is symmetrical!";
}
else {
return "The word is not symmetrical";
}
}
console.log("meat");