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?
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);
+ 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" ;)
+ 1
Thanks Haris but I think it was me, not fully understanding the contents of the book I read.
+ 1
No problem.
We learn from mistakes and unexpected behaviour in codes. âș
+ 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 ;)
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.)
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 */
0
Yep, I copied the code in the book and it works for them, just not for SoloLearn.
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.
0
Thanks visph, the book I read did mentioned some JSON methods and it seems to work now, thanks!