0
\$\begingroup\$

I have a for of loop, for each item in the childElements HTMLCollection I want to subtract the height of the elements from the parent height.

let height = element[0].clientHeight;
const childElements = <[HTMLElement]>element[0].children;
let i = 0;
for (const childElement of childElements) {
    if (i !== childElements.length - 1) {
        height -= Math.ceil(childElement.clientHeight);
    }
    i++;
}
return height;

But I want to exclude the last HTMLElement in the collection from the for loop. I've now created a i variable that is checked against the length of the collection minus 1. If that's not the case the height of the element is subtracted and i is increased.

This works fine, but it feels like I'm doing two things (for of loop and while loop). Any suggestions?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It hit me,

function returnSuggestedColleaguesWithExpertiseHeight(): number {
    let height = element[0].clientHeight;
    const childElements = <[HTMLElement]>element[0].children;
    for (let i = 0; i < childElements.length - 1; i++) {
        height -= Math.ceil(childElements[i].clientHeight);
    }
    return height;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.