[FIXED] Add every element in array to template literal


There is a function called calculateElementHeight that can take an array of offsets. Basically I want to get all the elements in the array of offsets and use that to return a template literal string from the function.

const calculateElementHeight = (offset: string[]) => {
// Code to get all the elements in the offset array and put a "-" in front of them

// returns `calc(100vh - all elements in offset array)`


For example, if the offset array for put is ['10px', '20px', '30px'], the calculateElementHeight function should return . I have. calc(100vh - 10px - 20px - 30px).Note: the offset array contains different units (i.e. ['10px' '20%', '30em']) This should be taken into account as it can

How can I achieve this?


Prefix all offsets with " - " to combine them. It also works if the list is empty.

const calculateElementHeight = (offset) => {
    return `calc(100vh${offsets.map((offset) => " - " + offset).join("")})`;

console.log(calculateElementHeight(["10px", "20px", "30px"]));

Answered By – vr.

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published