DEV Community

Discussion on: Square a number: awful answers only

Collapse
 
yoursunny profile image
Junxiao Shi

We need some bloated JavaScript libraries.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
* { border-spacing:0; margin:0; padding:0; }
body { width:1000000px; overflow:scroll; }
</style>
<script>
document.write('<script src="https://code.jquery.com/jquery.js"></' + 'script>');
</script>
<script>
function square(n) {
  const $table = $('<table>').attr('border', 0).appendTo($('body'));
  for (let row = 0; row < n; ++row) {
    const $tr = $('<tr>').appendTo($table);
    for (let col = 0; col < n; ++col) {
      $('<td>').attr({width: 100, height: 100}).appendTo($tr);
    }
  }

  let values = [];
  for (const prop in $table) {
    if (_.includes(['width', 'height'], prop)) {
      values.push($table[prop].call($table));
    }
  }
  return Math.round(Math.exp(_.sumBy(values, (value) => Math.log(value))) / 10000);
}

$(function() {
  $.getScript('https://unpkg.com/lodash@4.17.20/lodash.js', () => {
    console.log(square(4));
    console.log(square(16));
    console.log(square(51));
  });
})
</script>
Enter fullscreen mode Exit fullscreen mode