-3

So this is my code

let user = {
    fName: 'Kado',
    lName: 'Kliewer',
    uName: 'kKliewer23',
    age: 18,
};

function helloUser(user) {
    if(user.fName === true && user.lName === true && user.age >= 18) {
        console.log(`Hello ${user.uName}. Welcome to the world of wonders.`);
    } else {
        console.log("Please get a parent's permission before playing this game.")
    }
};

helloUser(user);

I'm brand new to coding and JavaScript but I'm a little confused on the relationship between objects and functions. When I run my code it logs "Please get a parent's permission before playing this game.

I have tried making the first part of my if/else statement to equal true but it evaluated to the same answer. Could someone please explain this and help me?

tadman
  • 208,517
  • 23
  • 234
  • 262
  • `user.fName === true` - This condition is `false`. (As is `user.lName === true`, for the same reason.) What condition are you *trying* to test for there? – David Jan 17 '23 at 18:38
  • You probably mean `if (user.fName && ...)` as `"Kado"` is not `true`. – tadman Jan 17 '23 at 18:40
  • I am trying to practice writing and understanding how JavaScript works. I was under the impression that every string value will equate to false, however when I entered that I didn't get the result I planned. The reason I want it to equal true or false is because someone else could enter a different uName or fName or even lName so I'm looking to make sure that is is a string not a number. – kado-kliewer23 Jan 17 '23 at 18:48
  • 3
    Does this answer your question? [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – about14sheep Jan 17 '23 at 18:50

2 Answers2

1

When you use user.fName === true && user.lName === true && user.age >= 18

It means : user.fName equals to true and user.lName equals to true

Since user.fName = "Kado", etc, this condition is unsuccessful hence it goes into the else

Rohit Khanna
  • 693
  • 5
  • 15
0

The problem is in if statement condition:

if(user.fName === true && user.lName === true && user.age >= 18)

It says if user.fName is equal to boolean true and user.lname is equal to boolean true and user.age is at least 18. And if all of these conditions are true console.log(`Hello ${user.uName}. Welcome to the world of wonders.`); will work otherwise else statement.

If you want to check if user has fname, lname, and age >=18 then this condition would work:

function helloUser(user) {
    if(user.fName && user.lName && user.age >= 18) {
        console.log(`Hello ${user.uName}. Welcome to the world of wonders.`);
    } else {
        console.log("Please get a parent's permission before playing this game.")
    }
};
jkalandarov
  • 564
  • 5
  • 15