Skip to content

Commit

Permalink
enhanced error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tbtz committed Dec 3, 2017
1 parent d2e3ac0 commit 1d349cb
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,35 @@ function formatDate(date) {
}

function generateOutputText(meals, date) {
console.log(date);
let dateString = formatDate(date);
let outputText = 'Hier ist der Essensplan für ' + dateString + '. '

for (var key in meals) {
let mealGroup = key;
outputText += 'In der Kategorie ' + mealGroup + ' gibt es: ';
mealsArray = meals[key];

mealsArray.forEach((meal, index) => {
if (mealsArray.length === 1) {
outputText += meal + '. '
} else if (index == mealsArray.length - 1) {
outputText += ' und ' + meal + '. '
} else if (index > 0) {
outputText += ', ' + meal
} else {
outputText += meal;
}
})
}
let outputText = '';

if (Object.keys(meals).length === 1 && Object.keys(meals)[0] === '') {
outputText = dateString + ' gibt es leider kein Speisenangebot.';
} else {
outputText = 'Hier ist der Essensplan für ' + dateString + '. '

for (var key in meals) {
let mealGroup = key;
outputText += 'In der Kategorie ' + mealGroup + ' gibt es: ';
mealsArray = meals[key];

mealsArray.forEach((meal, index) => {
if (mealsArray.length === 1) {
outputText += meal + '. '
} else if (index == mealsArray.length - 1) {
outputText += ' und ' + meal + '. '
} else if (index > 0) {
outputText += ', ' + meal
} else {
outputText += meal;
}
})
}

outputText = outputText.replace(new RegExp(' , ', 'g'), ', ');
outputText = outputText.replace(new RegExp(' . ', 'g'), '. ');
outputText = outputText.replace(new RegExp(' , ', 'g'), ', ');
outputText = outputText.replace(new RegExp(' . ', 'g'), '. ');
}
return outputText;
}

Expand Down

0 comments on commit 1d349cb

Please sign in to comment.