Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  137] [ 1]  / answers: 1 / hits: 126238  / 14 Years ago, fri, november 26, 2010, 12:00:00

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#.


More From » enums

 Answers
20

Do you actually need the numeric values at all? If not then you could use something like this:



var TEST_ERROR  = {
SUCCESS : 'Success',
FAIL : 'Fail',
ID_ERROR : 'ID Error'
};

[#94834] Wednesday, November 24, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
beatriceisabelad

Total Points: 710
Total Questions: 107
Total Answers: 99

Location: Cayman Islands
Member since Sat, Sep 17, 2022
2 Years ago
beatriceisabelad questions
Fri, Apr 2, 21, 00:00, 3 Years ago
Fri, Jun 19, 20, 00:00, 4 Years ago
Tue, Dec 3, 19, 00:00, 5 Years ago
Wed, Oct 16, 19, 00:00, 5 Years ago
;