The problem is that when you use return 'pork'
or whatever return
, your function actually returns, ie quits the function and never examine the other conditions.
The solution is simple : use a separate variable to concatenate results and return that variable at the end of the function.
Something like this :
function proteins(inCell){
var result = '';
if(inCell.toString().match('chicken') == 'chicken') {
result += 'chicken';}
else if(inCell.toString().match('mortadella') == 'mortadella') {
result += 'pork';}
else if(inCell.toString().match('beef') == 'beef') {
result += 'beef';}
else if(inCell.toString().match('pork') == 'pork') {
result += 'pork';}
else if(inCell.toString().match('ham') == 'ham') {
result += 'pork';}
else {result += 'something else';}
return result;
}
This will return all the valid strings put together, you might want to add separators for readability then simply add a comma or an hyphen after each item you add...
And if you really want it to be perfect, remove the last comma before returning the result using a simple javascript string function. See doc here for example