0

Hey there I am new to javascript.

I am working with a API but can not convert the JSON boolean to YES or NO.

The boolean is at Phone that default is false. So I am trying to return false to "NO"

Here is my code:

        function loadCustomer(e) {
        const xhr = new XMLHttpRequest();

        xhr.open('GET', 'https://jsonplaceholder.typicode.com/todos/1', true);

        xhr.onload = function () {
            if (this.status === 200) {
                // console.log(this.responseText);

                const person = JSON.parse(this.responseText);


                const output = `
                    <ol>
                    <li>ID: ${person.userId}</li>
                    <li>Name: ${person.id}</li>
                    <li>Company: ${person.title}</li>
                    <li>Phone: ${person.completed}
                    </li>
                    </ol>
                `;

                document.getElementById('customer').innerHTML = output;
                // console.log(customer.completed);
                // if (customer.completed = false) {
                //     output.innerHTML = 'hey'
                // }

            }
        }

        xhr.send();
    }

Here is the JSON:

{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

1
  • Use double equal sign to compare: if(customer.completed == false), otherwise, you're asigning false. Commented Dec 17, 2019 at 21:26

2 Answers 2

4

you can try this :

<li>Phone: ${person.completed ? 'YES' : 'NO'}</li>
Sign up to request clarification or add additional context in comments.

Comments

0

To display "YES" or "NO" instead of true and false, try:

    function loadCustomer(e) {
    const xhr = new XMLHttpRequest();

    xhr.open('GET', 'https://jsonplaceholder.typicode.com/todos/1', true);

    xhr.onload = function () {
        if (this.status === 200) {
            // console.log(this.responseText);

            const person = JSON.parse(this.responseText);

            var phone = "NO";
            if (customer.completed) {
                phone = "YES";
            }
            const output = `
                <ol>
                <li>ID: ${person.userId}</li>
                <li>Name: ${person.id}</li>
                <li>Company: ${person.title}</li>
                <li>Phone: {PHONE}
                </li>
                </ol>
            `.replace(/{PHONE}/g, phone);

            document.getElementById('customer').innerHTML = output;
            // console.log(customer.completed);
            // if (customer.completed = false) {
            //     output.innerHTML = 'hey'
            // }

        }
    }

    xhr.send();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.