In Javascript is it possible to convert many if/else statements to a dictionary for space efficiency?
For example I have the following which is longer in reality
if (namelen < 20) {
form.innerHTML = 'hurray!';
}
else if (namelen > 20 && lastname < 90) {
form.innerHTML = 'too long';
}
else if (namelen > 90 && lastname < 120) {
form.innerHTML = 'go away you cheater';
}
...
Is there a way to make that into a dictionary for space efficiency?
maybe something like
var diction = {namelen < 20: 'hurray', namelen > 20: 'too long', lastname < 90: 'too long', namelen > 90: 'go away you cheater', namelen < 120: 'go away you cheater'}
If not a dictionary, how would I refactor that many if else? bear in mind that I'm new to JS and can have missed some important features, and before you ask I spend most of the day reading programming books and the rest of the day coding.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…