w3resource

JavaScript: Test whether the character at the provided character index is upper case

JavaScript String: Exercise-43 with Solution

Write a JavaScript function to test whether the character at the index provided is upper case.

Test Data:
console.log(isUpperCaseAt('Js STRING EXERCISES', 1));
false

Visual Presentation:

JavaScript: Test whether the character at the provided character index is upper case

Sample Solution:

JavaScript Code:

// Function to check if the character at a specified index in a string is uppercase
function isUpperCaseAt(str, index) {
    // Check if the character at the specified index, converted to uppercase,
    // is equal to the character at the specified index
    return str.charAt(index).toUpperCase() === str.charAt(index);
}
// Log the result of calling the isUpperCaseAt function with the input string 'Js STRING EXERCISES' and index 1
console.log(isUpperCaseAt('Js STRING EXERCISES', 1));

Output:

false

Explanation:

In the exercise above,

  • The function "isUpperCaseAt()" takes two parameters: 'str' (the input string) and 'index' (the index of the character to check).
  • Inside the function, it uses the "charAt()" method to retrieve the character at the specified index in the string.
  • It then uses the "toUpperCase()" method to convert the character to uppercase.
  • Finally, it compares this uppercase character to the original character at the specified index. If they are equal, it means the character at that index was originally uppercase.
  • The function returns 'true' if the character at the specified index is uppercase, and 'false' otherwise.

Flowchart:

Flowchart: JavaScript: Test whether the character at the provided character index is upper case

Live Demo:

See the Pen JavaScript Test whether the character at the provided character index is upper case-string-ex-43 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to uncapitalize each word in the string.
Next: Write a JavaScript function to test whether the character at the provided character index is lower case.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-43.php