0
How can I make this successful display the line number?
I can't seem to get it to display the line number. I tried using stack then matching the line number surrounded by colons, but it failed xD. https://code.sololearn.com/WiLEh0T4VL8V/?ref=app
10 Respuestas
+ 3
Ah. To extract the linenumber with the regex, this will return an object for the first numbers-inside-colons found:
var linenum = str.match(/:([0-9]*):/);
Then just reference item [1].
... " (Line " + linenum[1] + ")";
I've deleted my other answer because you're asking for it to display 5, which implies you are using a browser (where lines are correct) and NOT using the app (where lines may be offset/incorrect).
In the app, line numbers will be offset (e.g., I see an error at line 34) due to the way SoloLearn injects the tabs.
I have code to autofix this offset and even identify the tab, but I'll wait for someone to need it.
+ 2
Actually, if I understand...l think you needed to change the regex to (group) the answers. The (parentheses) allowed me to auto-exclude the colons, as you wanted in another Q&A. From memory, I believe typically you'd use this with a {backreference} like: ([0-9]*){1} for "I want match 1 from the last (group)" but I don't think it's required here.
For the line number offset...
In app, I usually see the offset equal to the line count of the [JS] and [CSS] tabs and +1/+2 for the injected tags. You can evaluate the injection via:
alert(document.all[0].outerHTML);
...which should allow you to write a formula if you scan the tags' linecounts by their IDs.
Note, you can't really hardcode the offset. A general solution will need to scan <head> and account for body scripting too (and on DOM ready, which is why I wrote an includable module).
+ 1
Sololearn concatenates your JavaScript into HTML, before run it on your device, the line number refer to the working HTML, not the line number of Sololearn JavaScript code.
Perhaps you could offset it by substrate the result number by 29.
+ 1
Actually, I'm using the app. I decided to try this AFTER seeing the error on laptop, but I didn't get a chance to try it on my laptop. So does that mean I was probably correct the first time, but the app made it seem like I was doing it wrong?
+ 1
Here's the original code I wrote from my phone when I first discovered the problem. When I just ran it on the laptop, the line number was correct. Now all I need to do is solve the second problem, but i'll figure it out tomorrow. Thanks for your help!
https://code.sololearn.com/W8ZuFAhiWdHX/#js
+ 1
https://code.sololearn.com/Wna1EqDSpJ04/#js
Check out the finished code. Thanks for the help everyone.
0
I don't see any problems. Did you fix it just now?
0
No. It's not correctly displaying the line numbers.
0
No ODLNT . I'm asking how I can display the 5 by itself.
0
Kirk Schafer I actually made a better version that doesn't replace the entire string, but instead displays the error then adds the line number at the end.
This version doesn't replace the colon after "ReferenceError".
https://code.sololearn.com/WWRIhRmXDDhS/?ref=app