Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

SlightlyLoony
Tera Contributor

find_real_file.pngWhat value will the following script print in the log?


var a = 'snake 31';
var b = /[0-9]+/.exec(a);
b += 2;
gs.log(b);

Open this post to get the answer...

The answer is: 312.

The first tricky bit is the regular expression in the second line. This simply extracts the number from from the variable "a", and returns a 31. But...the value stored in "b" is a string, not a number.

The second part that might trip you up is the addition in the third line. This simply concatenates the string "31" (in variable "b") with a 2. JavaScript converts the number 2 into the string "2" to do this, but only because the value in "b" is a string. If what you really wanted was to add the number 31 to the number 2 (to get 33), this script would accomplish that:

var a = 'snake 31';
var b = parseInt(/[0-9]+/.exec(a));
b += 2;
gs.log(b);