-
-
Save navanathjadhav/ae7196e643c7f796cedcbc6f0286d0d2 to your computer and use it in GitHub Desktop.
Code example with for and while
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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