Implement map and render to DOM

Quick Overview

This question evaluates a candidate's skills in vanilla JavaScript utility implementation and DOM rendering, covering array and plain-object iteration semantics, higher-order functions, input validation, error handling, and safe text rendering for browser environments.

Implement map and render to DOM

Company: LinkedIn

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: HR Screen

Using only vanilla JavaScript in a script tag, implement a function similar to Array.prototype.map (without using the built-in map) to transform a list (array or key–value dictionary) of objects. Then render one div per transformed item into the DOM with appropriate classes/attributes and handle empty/error states. Do not use any frameworks.

Quick Answer: This question evaluates a candidate's skills in vanilla JavaScript utility implementation and DOM rendering, covering array and plain-object iteration semantics, higher-order functions, input validation, error handling, and safe text rendering for browser environments.

|Home/Software Engineering Fundamentals/LinkedIn
LinkedIn logo
LinkedIn
Sep 6, 2025, 12:00 AM
mediumSoftware EngineerHR ScreenSoftware Engineering Fundamentals
4
0

Build a Vanilla JS map-like Utility and DOM Renderer

Context

You are working in a basic browser environment with no frameworks or libraries. Implement a function that behaves like Array.prototype.map but must:

  • Not use the built-in map method.
  • Work for both arrays and plain key–value objects ("dictionaries").

Then use it to transform a list of objects into a render-friendly shape and render one div per transformed item into the DOM, with appropriate classes/attributes. Handle empty and error states.

Requirements

  1. Implement a map-like function that:
    • Accepts an input list (array or plain object), a callback, and an optional thisArg.
    • For arrays: iterates items (skipping holes) and returns a new array of mapped results.
    • For objects: iterates own enumerable keys and returns a new array of mapped results.
    • Validates inputs and throws a helpful error if misused.
  2. Implement a renderer that:
    • Renders one div per transformed item into a container in the DOM.
    • Applies classes and any attributes provided by the transformed items.
    • Uses safe text rendering (no innerHTML for untrusted data).
    • Shows an empty state if there are no items.
    • Shows an error state if something goes wrong.
  3. Use only vanilla JavaScript in a script tag. No frameworks.
Loading comments...