Barclays Interview Question: Write a javascript function t... | Glassdoor.co.uk

Interview Question

Senior UI Developer Interview London, England

Write a javascript function to draw a chessboard

Answer

Interview Answer

3 Answers

2

Using a loop and mod function, obvs.

Interview Candidate on 29 Oct 2015
0

div.chess-row {
       height:35px;
     }
    div.chess-column {
     height: 35px;
    width: 35px;
    display: inline-block;
    border:solid 1px black;
    }
    div.chess-column.black-cell {
       background-color:black;
    }

  for(var i=1;i

Pravin Mohite on 22 Dec 2016
0

div.chess-row {
       height:35px;
     }
    div.chess-column {
     height: 35px;
    width: 35px;
    display: inline-block;
    border:solid 1px black;
    }
    div.chess-column.black-cell {
       background-color:black;
    }
Js:-

for(var i=1;i<=8;i++)
   {
       var chessRow=document.createElement("div");
       chessRow.classList.add("chess-row");
         chessRow.classList.add("chess-row_"+i);
      document.querySelector('body').appendChild(chessRow);

      for(j=1;j<=8;j++)
      {
            var chessColumn=document.createElement("div");
             chessColumn.classList.add("chess-column");
          if(((i+j)%2)==0)
          {
             chessColumn.classList.add("black-cell")
          }
           document.querySelector('body .chess-row_'+i).appendChild(chessColumn);
      }
    }

Pravin Mohite on 22 Dec 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.