I want to make conversion tool which converts one code (user input) to another (predefined). I decided to use Javascript object as a container for codes, and my function will take user input which is actually a key from a javascript object, match it to the one in Code container and if the match is found, the function will display value to the alert box.
I made one code, but it does not work. I tried to find the solution but for now, I failed.
Here is my code:
$(document).ready(function() {
$("#convert").click(function(){
var GardinerToUnicodeCodePoint = {
"A1" :"995328",
"A1A" :"995329",
"A1B" :"995330",
"A1C" :"995331",
"A2" :"995332",
"A2A" :"995333",
"A3" :"995334",
"A3A" :"995335",
"A3B" :"995336",
"A4" :"995337",
"A4A" :"995338",
"A4B" :"995339",
"A4C" :"995340",
"A4D" :"995341",
"A4E" :"995342",
"A5" :"995343",
"A5A" :"995344",
"A5B" :"995345",
"A5C" :"995346",
"A6" :"995347",
};
var userInput = $("#userInput").val; /*for example 'A1'*/
if (userInput in GardinerToUnicodeCodePoint) {
alert(/*value of key 'userInput' -> 995328*/);
} else {
alert("No code found!");
}
});
});
You can use []
after calling the object to get the key value pair:
GardinerToUnicodeCodePoint[userInput]
Change your code to:
var userInput = $("#userInput").val; /*for example 'A1'*/
if (userInput in GardinerToUnicodeCodePoint) {
alert(GardinerToUnicodeCodePoint[userInput]);
} else {
alert("No code found!");
}
See jsfiddle: https://jsfiddle.net/wy70s3gj/
function getReturnCodeUsingKey(keyFromUserInput)
{
var GardinerToUnicodeCodePoint = {
"A1" :"995328",
"A1A" :"995329",
"A1B" :"995330",
"A1C" :"etc"
};
returnVal = GardinerToUnicodeCodePoint[keyFromUserInput];
return returnVal ? returnVal : "error: no match found";
}
Pass that function your string input
from the user, and it'll return what you want I think.
So, you're full solution would look like this:
$(document).ready(function() {
$("#convert").click(function(){
var userInput = $("#userInput").val(); /*for example 'A1'*/
// a call to our new function keeping responsibilities seperated
return getReturnCodeUsingKey(userInput);
});
});
function getReturnCodeUsingKey(keyFromUserInput)
{
var GardinerToUnicodeCodePoint = {
"A1" :"995328",
"A1A" :"995329",
"A1B" :"995330",
"A1C" :"995331",
"A2" :"995332",
"A2A" :"995333",
"A3" :"995334",
"A3A" :"995335",
"A3B" :"995336",
"A4" :"995337",
"A4A" :"995338",
"A4B" :"995339",
"A4C" :"995340",
"A4D" :"995341",
"A4E" :"995342",
"A5" :"995343",
"A5A" :"995344",
"A5B" :"995345",
"A5C" :"995346",
"A6" :"995347",
};
// set a variable to hold the return of the object query
returnVal = GardinerToUnicodeCodePoint[keyFromUserInput];
//return valid value from object, or string if undefined
return returnVal ? returnVal : "error: no match found";
}
The issue here as expresed in the comments by @epascarello is that you should use $("#userInput").val();
with the parenthesis
Code example:
$('#convert').click(function() {
var GardinerToUnicodeCodePoint = {
A1: '995328',
A1A: '995329',
A1B: '995330',
A1C: '995331',
A2: '995332',
A2A: '995333',
A3: '995334',
A3A: '995335',
A3B: '995336',
A4: '995337',
A4A: '995338',
A4B: '995339',
A4C: '995340',
A4D: '995341',
A4E: '995342',
A5: '995343',
A5A: '995344',
A5B: '995345',
A5C: '995346',
A6: '995347'
};
var userInput = $('#userInput').val();
var result = userInput in GardinerToUnicodeCodePoint
? 'Value of key \'userInput\' -> ' + userInput
: 'No code found!';
console.log(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="userInput">
<button id="convert">Submit</button>