+ 2

Javascript console.log not working

Does anybody else have this problem? Console doesn't write anything, even simple variable log. Copied the code to atom and mozilla logs everything. Tried to console.log in sololearn with a laptop, dekstop pc and android phone and none of them work. Couple days back console.log worked fine, is there a setting i have mistakenly activated or something? Thanks edit:typos

2nd Aug 2018, 4:42 PM
Ville Keituri
Ville Keituri - avatar
11 Answers
+ 12
Works fine for me in the app. Maybe you did some mistakes while writing the code? Can you show it?
2nd Aug 2018, 5:11 PM
Nikhil
Nikhil - avatar
+ 4
Sorry i was not clear on my original post. So the events went like this. 1. Moticed on my laptop that the sololearn desktop site wont console log. 2. Went to my desktop pc and ran the same code in sololearn desktop site. Still no console log. 3. Copied the code to atom, ran it in firefox with console enabled and every code run also output the value to console. Now the code writes console.log just fine on sololearn phone app. Im on work now, will get back tomorrow if the problem persists. Maybe i did something wrong on the sololearn desktop site, or my code was bad. :D Anyway, thanks for the help!
2nd Aug 2018, 7:06 PM
Ville Keituri
Ville Keituri - avatar
+ 3
Both on app and web code playground, the output doesn't got automatically the focus... so your onkeydown event doesn't fire ^^ The manual solution is to touch the output zone (anywhere in web code playground, but in app you must touch inside the body content height, wich doesn't necessarly correspond to the viewport height ;)). The automatic solution is to call the window object focus() method anywhere in your code (the window object, unlike the document content is available as soon as JS run, so doesn't require to be embeded in a window.onload event): window.focus();
2nd Aug 2018, 5:35 PM
visph
visph - avatar
+ 3
1> advice: I suggest you to better indent your code, so you could see more easily what is inside what, for both html and js 2> chrome support inlined js event, but your "move" function is unreachable from global scope since you define it inside the anonymous function assigned to the document.onload event ^^ 3> try to remove the "z-index:-1;" line from the #box css definition: it seems to have no effect in the app (on android webview at least), but in web browser (android chrome at least) it seems to make the element unreachable to user click (I guess because it would be under the "body" element wich cover it now and don't let the event traverse it ^^) 4> sololearn web code playground console log just fine for me on my tablet android/chrome, but on desktop/firefox effectively the SL console doesn't log (but that's not a problem, as on desktop there's a "real" browser console handler better suitable ^^) 5> last advice: as soon as the box doesn't respond to user click/touch, don't be surprised to not get log, because your function is probably never called/found by the event handler... (my first attempts in such case is to add a simple "alert('test')" -- if no risk of infinite alert repeat -- or "console.log('test')" as first statement of the function -- no var, just a string literal as argument -- to test if the function is called, and if not, comment all the function body rest to prevent unexpected error wich could cause the function to not be executed even if called)
3rd Aug 2018, 1:01 PM
visph
visph - avatar
2nd Aug 2018, 5:19 PM
Ville Keituri
Ville Keituri - avatar
+ 2
If on your "desktop pc" means "not in the web version of sololearn code playground" but "with a local or internet hosted file", so the console isn't displayed automatically: you'll have to open the developper tools pane through menu or with key shorcut... for firefox, the console shortcut is Ctrl + Maj + K (window/linux) Cmd + Opt + K (apple/mac). However, in the SL web code playground the console is displayed below the output zone, and should be scrolled to view more than just few lines ;) Anyway, the 'onclick' event fire just fine, because focus is automatically set to the clicked element ^^
2nd Aug 2018, 6:23 PM
visph
visph - avatar
+ 1
Yes you are right i need to touch or click the zone in order for it to work but once the box element starts to move a random number of pixels to the right when pressing any buttons, the console should start to fill but i dont get any output.
2nd Aug 2018, 5:53 PM
Ville Keituri
Ville Keituri - avatar
+ 1
I don't really understand your last post: have you still a problem? Once the output have got focus, pressing key will run code as expected: move randomly the box in the range specified (50) and log the new style.left value assigned... If you want to move the box each time more to the right instead of keep the box in between 0 and 50px from the left, you should add the new randomly computed value to the previous: box.style.left = parseInt(box.style.left||0)+Math.floor(Math.random()*50)+ "px"; (the "||0" is for avoiding NaN result at first event call, as initially the value of box.style.left is an empty string wich will be parsed as NaN)
2nd Aug 2018, 6:09 PM
visph
visph - avatar
+ 1
The problem still persist. Im on my laptop now and still i can't get any console log in sololearn desktop site. I tried mozilla and chrome, both does not work. On my phone when using the sololearn android app, it console logs fine. Here's the code = https://code.sololearn.com/WrDxZm8h78SQ/#js I had to change the box.onclick to addEventListener('click', f()) since chrome does not support inline javascript. edit: Now i tried to open mozillas own console and it logs the value in there just fine. The sololearn dekstop sites own javascript log does not work.
3rd Aug 2018, 12:12 PM
Ville Keituri
Ville Keituri - avatar
+ 1
Thank you for advice. I tried to edited my code to more user friendly format. Your advice no.4 was what i was asking for, the mozilla console is good and all but takes unnecessary space from my browser when there is also SL console available :) the z-index was unnecessary, i wasnt seeing the press text in my box element so i tried to lower the z-index in order to make the text appear... turned out it was a error in my code elsewhere. :D Anyway, thanks for advice!
3rd Aug 2018, 2:04 PM
Ville Keituri
Ville Keituri - avatar
0
Hmm, i changed to different phone and changed the onkeydown listener to onclick and now the console works fine. But on my desktop pc with the original code posted in this question, i dont get any console logs.
2nd Aug 2018, 6:13 PM
Ville Keituri
Ville Keituri - avatar