D
D
Dmitry2020-11-16 10:44:07
JavaScript
Dmitry, 2020-11-16 10:44:07

How to output JSON as a valid string?

Unable to complete the task: In the last-bottom field, write the code for the task(x) function that returns JSON as a valid string (which is the result of applying the JSON.stringify function to the object);
The original object must have a love property with a javascript string value, a year property with a numeric value of 2020, and an arg property with an x ​​value.

function task(x) {
let user = {
love: 'javascript',
year: 2020,
arg: x
}
console.log(JSON.stringify(user, null, 2))
}

function task(x) {
let user = {
love: 'javascript',
year: 2020,
arg: x
}
console.log(JSON.stringify(user))
}

I tried these two options, but the task is not counted with the wording "The expected result is NOT produced". Maybe professionals will tell you where the error is or it was necessary to use a different line output than mine. The word "correct" in the assignment is especially confusing ... thanks in advance for the answer ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tim, 2020-11-16
@Daemon1976

return

function task(x) {
    let user = {
        love: 'javascript',
        year: 2020,
        arg: x
    }
    return JSON.stringify(user);
}

short entry
const task = x => JSON.stringify({love: 'javascript',year: 2020,arg: x});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question