I am encountering an issue with my JavaScript code while working on a reminder app in Cordova and using the Katzer notification plugin. My goal is to implement a feature where, if a user tries to add a reminder that already exists, an error is thrown. Conversely, if the reminder does not exist, it should be added to the list of reminders. I am currently facing a challenge with my JavaScript loop in achieving this. Here is a snippet of my code:
function checkIfReminderExists(){
cordova.plugins.notification.local.getAll(function (notifications) {
var allRemindersInfo = "";
var newAllRemindersInfo = "";
// using a while loop
var count = 0;
while (count < notifications.length) {
cordova.plugins.notification.local.get(count, function (notification) {
allRemindersInfo = allRemindersInfo + notification.text;
if(allRemindersInfo.indexOf(""+checkedBoxes+"") == true) {
alert("Sorry, you cannot add a reminder that already exists...");
} else {
alert("There is no similarity, so I am going ahead to create the reminder now...");
setLecReminders();
}
});
count++;
continue;
}
});
}
/* The above method did not work, so I tried using a for loop to address the issue */
function checkIfReminderExists(){
cordova.plugins.notification.local.getAll(function (notifications) {
var allRemindersInfo = "";
var newAllRemindersInfo = "";
var count;
for(count = 0; count < notifications.length; count++) {
cordova.plugins.notification.local.get(count, function (notification) {
allRemindersInfo = allRemindersInfo + notification.text + ", ";
newAllRemindersInfo = new Array(""+allRemindersInfo+"");
if(newAllRemindersInfo.indexOf(""+checkedBoxes+"") == true) {
alert("Sorry, you cannot add a reminder that already exists...");
} else {
alert("There is no similarity, so I am going ahead to create the reminder now...");
setLecReminders();
}
});
}
});
}
I have tried both the for and while loops mentioned above, but unfortunately, none of them provided the desired result. Instead, the "if()else" test runs separately for each loop iteration. This causes a challenge where the setLecReminders() function executes regardless of the subsequent test results, leading to undesired outcomes. I am seeking a solution where the loop completes first, all items in the list are stored in an array, and then a simultaneous if()else
test can be performed on all members of the array. Apologies for the lengthy question and thank you in advance.