1414// You will need to come up with an appropriate name for the function
1515// Use the MDN string documentation to help you find a solution
1616// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17+
18+ // Declare a function called toUpperSnakeCase with named parameter inputString
19+
20+ function toUpperSnakeCase ( inputString ) {
21+ // Convert the input string to uppercase
22+ const upperCaseString = inputString . toUpperCase ( ) ;
23+ // Replace spaces with underscores
24+ const snakeCaseString = upperCaseString . replace ( / / g, '_' ) ;
25+ return snakeCaseString ;
26+ }
27+
28+ // Call the function with different inputs to check it works
29+ console . log ( toUpperSnakeCase ( "hello there" ) ) ; // Output: "HELLO_THERE"
30+ console . log ( toUpperSnakeCase ( "lord of the rings" ) ) ; // Output: "LORD_OF_THE_RINGS"
31+ console . log ( toUpperSnakeCase ( "javascript is fun" ) ) ; // Output: "JAVASCRIPT_IS_FUN"
32+ console . log ( toUpperSnakeCase ( "test case" ) ) ; // Output: "TEST_CASE"
33+ console . log ( toUpperSnakeCase ( "multiple words here" ) ) ; // Output: "MULTIPLE_WORDS_HERE"
34+
35+ //can also test with edge cases
36+ console . log ( toUpperSnakeCase ( "" ) ) ; // Output: ""
37+ console . log ( toUpperSnakeCase ( "singleword" ) ) ; // Output: "SINGLEWORD"
38+ console . log ( toUpperSnakeCase ( " leading and trailing spaces " ) ) ; // Output: "___LEADING_AND_TRAILING_SPACES___"
39+
40+ // Alternative solution using replaceAll method
41+ function toUpperSnakeCase ( str ) {
42+ return str . toUpperCase ( ) . replaceAll ( " " , "_" ) ;
43+ }
44+
45+ // Differences between the two methods:
46+ // 1. The first method uses a regular expression with the replace method
47+ // to replace all spaces, while the second method uses the replaceAll
48+ // method which is more straightforward for this specific case.
49+ // Regex is a more complex tool that can be used for more advanced
50+ // string manipulation and "replaces all matches globally", while the
51+ // replaceAll methods is simpler and more efficient for replacing all
52+ // occurrences of a specific substring.
53+ // 2. The replaceAll method is a newer addition to JavaScript and may
54+ // not be supported in older environments, while the replace method
55+ // with a regular expression is widely supported.
0 commit comments