Search code examples
javascriptstringfunctionnumberscycle

Validate title function


Write a title validation function - validateTitle, which takes an argument and validates it by the rules:

  • Title length must be less than 2 characters but less than 20.

  • Title must start with an uppercase letter

  • Function should return ‘VALID’ if the string meets the requirements or ‘INVALID’ if it does not. And return 'Incorrect input data' if the given argument not a string. Do not use regular expressions. My solution is not full

      const validateTitle = (value) => {
      if (typeof value !== "string") {
        return "Incorrect input data";
      }
      for (let i = 2; i <= 19; i++) {}
    };
    

    Examples: validateTitle(false) // 'Incorrect input data' validateTitle([]) // 'Incorrect input data' validateTitle('s') // 'INVALID validateTitle('12title') // 'INVALID' validateTitle('Title!') // 'VALID' validateTitle('Title?') // 'VALID'


Solution

  • function validTitle(tempString){
     let validFlag = ((tempString.charAt(0) == tempString.charAt(0).toUpperCase()) && 
                     (tempString.length > 2 && tempString.length < 20 )) ? true : false;
      return validFlag ? 'VALID' : 'INVALID'
    }
    

    try this -