Learn Functional Programming by Building a Spreadsheet - Step 17

Tell us what’s happening:

I’m having trouble with the attributes in the input element. I’ve done document.createElement(“input”), but I need help proceeding from there.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="./styles.css" />
    <title>Functional Programming Spreadsheet</title>
  </head>
  <body>
    <div id="container">
      <div></div>
    </div>
    <script src="./script.js"></script>
  </body>
</html>
/* file: styles.css */
#container {
  display: grid;
  grid-template-columns: 50px repeat(10, 200px);
  grid-template-rows: repeat(11, 30px);
}

.label {
  background-color: lightgray;
  text-align: center;
  vertical-align: middle;
  line-height: 30px;
}
/* file: script.js */
const range = (start, end) => Array(end - start + 1).fill(start).map((element, index) => element + index);
const charRange = (start, end) => range(start.charCodeAt(0), end.charCodeAt(0)).map(code => String.fromCharCode(code));

window.onload = () => {
  const container = document.getElementById("container");
  const createLabel = (name) => {
    const label = document.createElement("div");
    label.className = "label";
    label.textContent = name;
    container.appendChild(label);
  }
  const letters = charRange("A", "J");
  letters.forEach(createLabel);
  range(1, 99).forEach(number => {
    createLabel(number);

// User Editable Region

    letters.forEach(letter => {
      const input = document.createElement("input");
    })

// User Editable Region

  })
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Learn Functional Programming by Building a Spreadsheet - Step 17

To set value for an attribute of a DOM element, you can use syntax:

element.attributeName = attributeValue;

Example: to set name attribute for input (DOM element) with the value "fullname":

input.name = "fullname";

I finally got the syntax.

1 Like

I understood the .type syntax but did not understand how and why letter and number variables were set inside the .id

Hi there,

Do you mean you don’t understand why we set the id attribute of the input element to letter + number?

It’s because the instruction asked us to do so:

Set the type attribute to text and the id attribute to letter + number.

The purpose of the block of code we’re writing is:

We’re iterating letter from "A" to "J", and iterating number from 1 to 99.
At each iteration, we create a new input element and set its id to: letter + number.

For example:

At the iteration when letter is "A", and number is 2,
we create a new input element with id is: "A2".
("A" + 2 = "A2")

That’s what we’re doing.