Variable Name
Description
Correct variable names consist only of English letters, digits and underscores and they can’t start with a digit.
Check if the given string is a correct variable name.
Example
-
For name =
"var_1__Int"
, the output should bevariableName(name) = true
; -
For
name = "qq-q"
, the output should bevariableName(name) = false
; -
For
name = "2w2"
, the output should bevariableName(name) = false
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] string name
Guaranteed constraints:
1 ≤ name.length ≤ 10
.
- [output] boolean
true
if name
is a correct variable name, false
otherwise.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
function variableName(name) {
return /^[a-zA-Z_]+[a-zA-Z0-9_]*$/.test(name);
}