Skip to main content
Solved

Change text properties during first occurrence in (nested) for loop

  • September 29, 2022
  • 1 reply
  • 460 views

Branislav_Toman

Hi, I am trying to change text properties only on a first occurrence in for loop as shown on a code sample below:

      for (let j = 0; j < countRows; j++) {
        console.log(j)
        const row = figma.createFrame()
            row.clipsContent = false
            row.name = 'Row ' + (j+1)
            row.layoutMode = "VERTICAL"
            row.primaryAxisSizingMode = "AUTO"
            row.counterAxisSizingMode = "AUTO"
            column.appendChild(row)
          
          const rowTextFrame = figma.createFrame()
          rowTextFrame.clipsContent = false
          rowTextFrame.name = 'Text ' + (j+1)
          rowTextFrame.layoutMode = "VERTICAL"
          rowTextFrame.primaryAxisSizingMode = "AUTO"
          rowTextFrame.counterAxisSizingMode = "AUTO"
          rowTextFrame.paddingTop = 10
          rowTextFrame.paddingBottom = 10
          rowTextFrame.paddingLeft = 14

           row.appendChild(rowTextFrame)

          // Generate text

          if (j = 0) {
            const rowText = figma.createText();
            rowText.fontName = { family: "Open Sans", style: "Bold" }
            rowText.characters = "Headline";
            rowTextFrame.appendChild(rowText);  
          } else {
            const rowText = figma.createText();
            rowText.fontName = { family: "Open Sans", style: "Regular" }
            rowText.characters = "Edit me";
            rowTextFrame.appendChild(rowText);  
           } 

I added console.log(j) to see the output, so I am using my if conditional right
image

And then I added condition if (j = 0) (because the first occurrence would be at 0 always), but when executed Figma will freeze and console output looks like this.
image

It gets stuck on the next loop and whole Figma freezes and I need to manually close it.
What am I doing wrong? Why I cannot use simple IF conditional to execute different code on first for loop occurrence?

Best answer by tank666

In the condition, you used an assignment operator, not a comparison operator.

View original

1 reply

tank666
  • 4873 replies
  • Answer
  • September 29, 2022

In the condition, you used an assignment operator, not a comparison operator.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings