Tuesday, May 4, 2021

Javascript Practice 19: JS Hero - String: length

 
Question: 
 
Write a function length that takes a string and returns the number of characters of the string.

Example: length('sun') should return 3.
 
 
 
 
Answer: 
 
function length (string) {

return string.length;

};
 
> function name is length
> it is a function expression
> parameter is string
> statement is return string.length; enclosed in curly brackets
> this function returns a statement that will provide the length of the argument / parameter - 'string'
> length property of a String object contains the length of the string
> length is provided in UTF-16 code units
> length property returns the number of code units in the string
> for an empty string, length is 0

No comments:

Post a Comment