Created
December 4, 2022 19:01
-
-
Save elijahmanor/31c245ca45cd7283197e799957434b4f to your computer and use it in GitHub Desktop.
Chat GTP: Show JavaScript code that will reverse a string and take the first 5 characters and uppercase them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseString(str) { | |
// Step 1: Use the split() method to return a new array | |
let splitString = str.split(""); // split() method splits a string into an array of substrings | |
// Step 2: Use the reverse() method to reverse the new created array | |
let reverseArray = splitString.reverse(); // reverse() method reverses an array in place | |
// Step 3: Use the join() method to join all elements of the array into a string | |
let joinArray = reverseArray.join(""); // join() method joins all elements of an array into a string | |
// Step 4: Use the slice() method to return the first 5 characters of the reversed string | |
let sliceString = joinArray.slice(0, 5); // slice() method extracts a part of a string and returns a new string | |
// Step 5: Use the toUpperCase() method to return the string in uppercase | |
let upperCaseString = sliceString.toUpperCase(); // toUpperCase() method returns the calling string value converted to uppercase | |
// Step 6: Return the reversed, uppercased string | |
return upperCaseString; | |
} | |
// Example: | |
reverseString("hello"); // will return "OLLEH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment