So I have this in the javascript for my page:
var TEST_ERROR = { 'SUCCESS' : 0, 'FAIL' : -1, 'ID_ERROR' : -2 };
And perform tests on functions in the page like so:
function test() { // Get the paragraph from the DOM var testResultParagraph = document.getElementById('testResult'); // Check the paragraph is valid if(!testResultBox) { // Update the web page testResultParagraph.value = TEST_ERROR.ID_ERROR; return TEST_ERROR.ID_ERROR; } // Something to store the results var testResult = TEST_ERROR.SUCCESS; // Test the calculation testResult = testCalculate() // Update the web page testResultParagraph.value = testResult; // The test succeeded return TEST_ERROR.SUCCESS; }
The result of testCalculate()
and the value of the paragraph will be either 0, -1, -2 depending on the outcome.
Now I want to map this to a string so that the paragraph shows 'Success', 'Fail' or 'ID Error'
I could do this a few ways I have figured:
var TEST_ERROR = { 'SUCCESS' : {VALUE : 0 , STRING: 'Success' }, 'FAIL' : {VALUE : -1, STRING: 'Fail' }, 'ID_ERROR' : {VALUE : -2, STRING: 'Id Error'}, };
would require a modification to the enum dot accessors, or
var TEST_ERROR = { 'SUCCESS' : 0, 'FAIL' : 1, 'ID_ERROR' : 2 }; var TEST_STRING = [ 'Success', 'Fail', 'ID Error' ];
Which would require changes to the logic (result > TEST_ERROR.SUCCESS
seems wierd tho!)
My question is how would you go about mapping an enumerator value to a string value in Javascript? I'm thinking the second way is the most sensible, but would like the enumerator to be positive for successes and negative for fails. I also like the idea of the first containing the strings and values in the object structure.
Any ideas?
Thanks!
Matt
PS. I'm going to be doing the testing in a Web Worker, so that the page doesn't hang and the results will be put into a table, not a paragraph like above.
PPS. I'm pretty new to Javascript programming, but do a lot in ASM, C, C++, C#.