I just discovered the .test()
method. It returns true or false based on criteria.
The reason
I found the solution for a Code Arcade challenge as:
function solution(n) {
return /0[1-9]/.test(n);
}
The problem was to check to see if an integer’s roundness could be increased. At first I didn’t understand what that meant but the conclusion I reached was that we were testing to see if you could swap a 0 in a higher place value to one of a lower place value such that it would lead to having more trailing 0’s than it does currently. I got to the point where I could check its roundness and solved a couple of the test cases but only got has high as 8/10 solved before revealing the solutions.
If my understanding is correct, putting /text/ within the /‘s will search for that pattern. .text(n) is then taking /text/ and searching for it within ‘n’. So if we wanted to modify the above function to search to also include 0’s after the first, you could put /0[0][1-9]/ and it will search both things like the number ‘103’ and ‘1003’ whereas the first function only searched for ‘103’.