Your question is Code Review and Improvements. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Imagine another developer on Canva's editor team wrote the two functions below for managing layer order and deletion on a design canvas. Now you review it.
type Layer = {
id: string;
zIndex: number;
locked: boolean;
};
function bringLayerToFront(layers: Layer[], layerId: string): Layer[] {
const layer = layers.find((l) => l.id == layerId);
const maxZ = Math.max(...layers.map((l) => l.zIndex));
layer.zIndex = maxZ + 1;
let result = [];
for (let i = 0; i < layers.length; i++) {
if (layers[i].id !== layerId) {
result.push(layers[i]);
}
}
result.push(layer);
return result;
}
function deleteLayer(layers: Layer[], layerId: string) {
const index = layers.findIndex((l) => l.id === layerId);
layers.splice(index, 1);
return layers;
}
What do you look for in this review, and how would you improve it?