Recently, I encountered an issue while working with a variable in a Cypress each loop. Despite incrementing the variable within the loop, it resets to zero once outside of it. Can someone shed light on why this happens and suggest a solution? Thank you.
public getNoEntries(fullName: string): number
{
let noEntries: number = 0;
cy.get(this.employeeList).find('li').each((x) =>
{
var entryName = x.text().trim();
if (entryName.localeCompare(fullName) == 0)
{
++noEntries;
cy.log("in loop: " + noEntries.toString());
}
});
cy.log('out of loop: ' + noEntries);
return noEntries;
}
The current output:
in loop: 1 in loop: 2 in loop: 3 out of loop: 0
My goal is to have it return 3 consistently. Any suggestions on how to achieve this?
Thank you for your assistance.