Skip to content

Instantly share code, notes, and snippets.

@navanathjadhav
Created June 12, 2022 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navanathjadhav/ae7196e643c7f796cedcbc6f0286d0d2 to your computer and use it in GitHub Desktop.
Save navanathjadhav/ae7196e643c7f796cedcbc6f0286d0d2 to your computer and use it in GitHub Desktop.
Code example with for and while
/*
* Find first manager
*/
function findFirstManager(employees) {
let manager = {};
for (let i = 0; i < employees.length; i++) {
if (employees[i].role === "Manager") {
manager = employees[i];
// Break the loop after first manager is found, unnecessary execution is reduced
break;
}
}
// It will return first manager found in array
return manager;
}
/*
* Count HRs
*/
function countHRs(employees) {
let HRCount = 0;
let i = 0;
// Loop over employees till its length
while (i < employees.length) {
if (employees[i].role === "HR") HRCount++;
i++;
}
// Return count
return HRCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment