Code Playground couldn't work with objects in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Code Playground couldn't work with objects in JS?

I'm currently reading a book, and it has an exercise, in which, I have to convert an array to an object. I couldn't find the solution, so I searched instead the solution and find that in doesn't work in the CP (Code Playground) but in other browsers (or editors), it works. Is this a misfunction in the CP or it's just SoloLearn not able to work with this?

27th Mar 2018, 4:35 PM
Mioz
Mioz - avatar
10 Answers
+ 5
[object Object] is the normal output... Maybe some browsers could improve console.log() output to be more explicit... What you could do, would be to explicitly output JSON string of your object: console.log(JSON.stringify(object)); instead of: console.log(object);
27th Mar 2018, 4:48 PM
visph
visph - avatar
+ 3
It seems to work for me... Wich error message do you got? I guess that your device not support ES6 "let" keyword: try to replace them by "var" ;)
27th Mar 2018, 4:40 PM
visph
visph - avatar
+ 1
Thanks Haris but I think it was me, not fully understanding the contents of the book I read.
27th Mar 2018, 4:53 PM
Mioz
Mioz - avatar
+ 1
No problem. We learn from mistakes and unexpected behaviour in codes. ☺
27th Mar 2018, 4:55 PM
Haris
Haris - avatar
+ 1
For deeper information: When you write/log some object, JS try to convert it to string... the default string for an object is "[object Object]"... but you could customize it by defining a .toString() method to it, wich is called when object is required to be converted as string: function MyObject(prop) { this.prop = prop; this.toString = function() { return "this is an instance of MyObject, with property = "+this.prop; } } The JSON.stringify() method, is a quick way to log an object properties, but could show two much informations (on complex objects) or not some others expected ;)
27th Mar 2018, 5:00 PM
visph
visph - avatar
0
Solo isn't always working as a normal compiler/browser. It's probably Sololearn's fault. On the other hand I never encountered a problem with webcodes on solo. So I must ask did you code everything as the book said? (I didn't look at your code.)
27th Mar 2018, 4:43 PM
Haris
Haris - avatar
0
that's not it, let seems work to me. output I get is: [object Object] when the output should've been: list = { value: 1 rest: list { /* and so */
27th Mar 2018, 4:45 PM
Mioz
Mioz - avatar
0
Yep, I copied the code in the book and it works for them, just not for SoloLearn.
27th Mar 2018, 4:46 PM
Mioz
Mioz - avatar
0
Try it on brackets editor(or any other web editor), if it works there your code is fine. Sololearn is a platform for beginners and it's inherently limited at some aspects.
27th Mar 2018, 4:48 PM
Haris
Haris - avatar
0
Thanks visph, the book I read did mentioned some JSON methods and it seems to work now, thanks!
27th Mar 2018, 4:52 PM
Mioz
Mioz - avatar