Classes and constructors for null

class DisplayUserDetails{
  
  constructor(userName, password, confirmPassword){

    this.userName = userName
    this.password = password
    this.confirmPassword = confirmPassword

  }
  getUsername(){
    // console.log(this.userName)
    if (this.userName.length > 10){
      console.log(this.userName)
      return false
    }else{
    return true;
    }
    // console.log(this.userName)
}
  
  getPassword(){

  }
  confirmPassword(){

  }
}
let  userName = prompt("Enter your username")
const username = new DisplayUserDetails(userName, "Mbadady123", "Mbadady123")
console.log(username.getUsername());

hello campers,
please i want the above code to return false or something else anytime a user clicks on cancel on the prompt

class DisplayUserDetails{
  
  constructor(userName, password, confirmPassword){
if(userName === null){
  // throw new Error("Please enter a valid username")
  console.log("this is a null value");
  return;
}
    this.userName = userName
    this.password = password
    this.confirmPassword = confirmPassword

  }
  getUsername(){
    // console.log(this.userName)
    if (this.userName.length > 10){
      console.log(this.userName)
      return false
    }else{
    return true;
    }
    // console.log(this.userName)
}
  
  getPassword(){

  }
  confirmPassword(){

  }
}
let  userName = prompt("Enter your username")
// console.log(userName);
const username = new DisplayUserDetails(userName, "Mbadady123", "Mbadady123")
console.log(username.getUsername());

Now this works but throws an error for the below because null.length is not possible. Is it possible i can just return/end the program if user clicks on the cancel without having an error or without it running any other code inside my class

  if (this.userName.length > 10){
      console.log(this.userName)
      return false

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.