JavaScript: Test whether the character at the provided character index is lower case
JavaScript String: Exercise-44 with Solution
Write a JavaScript function to test whether the character at the given (character) index is lower case.
Test Data:
console.log(isLowerCaseAt ('Js STRING EXERCISES', 1));
true
Visual Presentation:
Sample Solution:
JavaScript Code:
// Define a function named isLowerCaseAt which checks if the character at a specific index in a given string is lowercase.
function isLowerCaseAt(str, index) {
// Retrieve the character at the specified index in the input string.
// Convert the character to lowercase and check if it's equal to the original character at that index.
return str.charAt(index).toLowerCase() === str.charAt(index);
}
// Print the result of calling the isLowerCaseAt function with the string 'Js STRING EXERCISES' and index 1.
console.log(isLowerCaseAt('Js STRING EXERCISES', 1));
Output:
true
Explanation:
In the exercise above,
- function isLowerCaseAt(str, index) {: This line declares a function named "isLowerCaseAt()" with two parameters: 'str' (the input string) and 'index' (the index of the character to check).
- return str.charAt(index).toLowerCase() === str.charAt(index);: This line checks if the character at the specified index in the input string, converted to lowercase, is equal to the original character at that index. It returns 'true' if the character is lowercase, and 'false' otherwise.
- console.log(isLowerCaseAt ('Js STRING EXERCISES', 1));: This line calls the "isLowerCaseAt()" function with the input string 'Js STRING EXERCISES' and index 1, and logs the result to the console. It checks if the character at index 1 (the second character) is lowercase.
Flowchart:
Live Demo:
See the Pen JavaScript Test whether the character at the provided character index is lower case-string-ex-44 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript function to test whether the character at the provided (character) index is upper case.
Next: Write a JavaScript function to get humanized number with the correct suffix such as 1st, 2nd, 3rd or 4th.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/javascript-exercises/javascript-string-exercise-44.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics