DEV Community

Discussion on: Square a number: awful answers only

Collapse
 
lucamattiazzi profile image
Luca

I think I found another one, even better:

const jsdom = require('jsdom')
const got = require('got')
const fs = require('fs')

const { random, floor } = Math

async function square(n) {
  const url = 'https://dev.to/mellen/square-a-number-awful-answers-only-1764/comments'
  const html = (await got.get(url)).body
  const dom = new jsdom.JSDOM(html, { runScripts: 'dangerously' })
  const scripts = dom.window.document.getElementsByClassName('javascript')
  const randomScript = scripts[floor(random() * scripts.length)]
  eval(randomScript.textContent)
  return square(n)
}

square(3).then((r) => console.log(r))
Enter fullscreen mode Exit fullscreen mode

this one works by searching inside the comments to this post any solution that was written in javascript (looking if the author has selected the js highlighter), then simply evals it and finally runs it

since it could be picked itself, it must be named square

Collapse
 
kidsmohamed profile image
kidsmohamed

thanks

Collapse
 
mellen profile image
Matt Ellen

So meta 😂

Collapse
 
itsjoekent profile image
Joe Kent

this is my favorite answer