w3resource

CoffeeScript function: Converts the first letter of each word of a string in upper case

CoffeeScript Function : Exercise-12 with Solution

Write a CoffeeScript function that accepts a string as a parameter and converts the first letter of each word of the string in upper case.

Example string : 'the quick brown fox'
Expected Output : 'The Quick Brown Fox '

HTML Code :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <title>Converts the first letter of each word of a string in upper case</title>
</head>
<body>

</body>
</html>

CoffeeScript Code :

uppercase = (str) ->
  array1 = str.split(' ')
  newarray1 = []
  x = 0
  while x < array1.length
    newarray1.push array1[x].charAt(0).toUpperCase() + array1[x].slice(1)
    x++
  newarray1.join ' '

console.log uppercase('the quick brown fox')

Sample Output:

"The Quick Brown Fox"

Live Demo :

See the Pen coffeescript-exercise-12 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.