Problem with either nested functions or quote marks?

Hey @jpegnight!

You identified the issue properly: you reached the nesting limit! Basically, you use way too many $(eval), you only need one.

Here’s how I’d write your code:

const idleResponses = `$(urlfetch json https://pastebin.com/raw/UmRNDXpa)`.split(`#`);
// $(urlfetch) isn't a native JS function, this is just for the example
const randomOutcome = Math.ceil(Math.random() * 10);
const goldAmount = Math.ceil(Math.random() * 100);
// the line above will generate a random number between 1 and 100 included
// if you'd rather generate a number between 0 and 100 included, use:
// Math.floor(Math.random() * 101)

if ('$(user)' === '$(touser)') {
  return '$(user) tried to spare themself. But nothing happened.';
}

if(randomOutcome === 10) {
  return `YOU WON! $(user) earned 0 XP and ${goldAmount} gold.`;
}

return '$(touser) ' + idleResponses[Math.floor(Math.random() * idleResponses.length)];

First I assign all the variables I’ll need, then I do the tests to get the right output.

So the Nightbot version is this:

$(eval i=`$(urlfetch json https://pastebin.com/raw/UmRNDXpa)`.split(`#`);r=Math.ceil(Math.random()*10);g=Math.ceil(Math.random()*100);if(`$(user)`==`$(touser)`){`$(user) tried to spare themself. But nothing happened.`}else{if(r==10){`YOU WON! $(user) earned 0 XP and ${g} gold.`}else{`$(touser)${i[Math.floor(Math.random()*i.length)]}`}})

You may notice I also switched from single quotation marks to backticks, that’s because un-escaped single quotation marks in your Pastebin break the code, and backticks are a good way to go around that, they allow me to foolproof the code a bit, and I wanted to remain consitstent.

1 Like