Я работаю над простым проектом, который проверяет, работает ли студент над онлайн-журналом. Прогресс студента проверяется с помощью идентификатора пользователя. Если userId учащегося не отображается в JSON, возвращаемом вызовом API для API класса, к контейнеру должен быть добавлен абзац, чтобы сообщить учащемуся, что он ничего не выполнил.
Вот пример типа данных, с которыми я работаю.
data = [
{ "userId": 101, "pagesDone": "005" },
{ "userId": 102, "pagesDone": "010" },
{ "userId": 103, "pagesDone": "020"},
{ "userId": 104, "pagesDone": "015" }
]
Теперь предположим, что я работаю со студентом с идентификатором пользователя 106. Этот студент не отображается в данных JSON, потому что он еще не начал работать над журналом.
let currentUserId = 106;
// Student has not completed anything in their journal
let showStudentJournal = (data, currentUserId) => {
for (var index = 0; index < data.length; index++) {
if (currentUserId == data[index].userId) {
// I have figured out the code here, basically I append a progressBar to a container to show the student's progress
// I won't show the code here since that's not what we're focusing on.
//The thing I want to happen is, get a paragraph to appear if the student hasn't filled out any of the online journal yet
} else {
$("#progressContainer").append('<p>You have not yet started your online Journal</p>');
}
}
}
Однако эта функция запускается 4 раза (потому что в данных содержится 4 ученика). Так вот что я получаю обратно:
'<p>You have not yet started your online Journal</p>'
'<p>You have not yet started your online Journal</p>'
'<p>You have not yet started your online Journal</p>'
'<p>You have not yet started your online Journal</p>'
Как заставить сообщение об ошибке появляться только один раз?