How to execute object methods | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

How to execute object methods

So I need to create an object with it properties and methods, so that methods work based on properties. The idea of the following code is that if the users age is under 18 he/she may not gain access to the website So I need a help to make this code work. var person={gender1:"male",gender2:"female",age:prompt("Please enter your age"), func: function accessSite(person[gender1],person[gender2],person[age]){ if(gender1==="male" || gender2==="female"&& age>=18){ alert("Access is gained");} else if (gender1==="male" || gender2==="female"&& age<18) { alert("You are not allowed to access this website"); }accessSite(); }; console.log(person.func());

26th Jun 2017, 8:39 PM
Manik Tafralian
Manik Tafralian - avatar
1 Resposta
+ 2
Take a look at this: https://www.w3schools.com/js/js_object_methods.asp Basically you can do the exact same thing they did, just with an added if-statement: var userAge = prompt("How old are you?"); functionĀ person(gender, age) { this.gender = gender; Ā Ā Ā Ā this.ageĀ = age; } var user = new person("male", userAge); if(user.age =>18){ alert("You can access"); } else{ alert("You can't access");
26th Jun 2017, 9:18 PM
Maart
Maart - avatar