Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge

Adam Stonehouse
Adam Stonehouse
4,645 Points

My attempt at the code challenge.

How could I capitalise the first letter of the pronouns without capitalising the other letters in the string?

alert('Welcome to Consequences, a series of questions will be posed and Consequences will adapt your answers into a story. Give it a try.');
var him = prompt('What was his name?');
console.log(him);
var her = prompt('What was her name?');
console.log(her);
var place = prompt('Where did they go?');
console.log(place);
var what = prompt('What did they do?');
console.log(what);
var heSaid = prompt('What did he say?');
console.log(heSaid);
var sheSaid = prompt('What did she say?');
console.log(sheSaid);
var happenings = prompt('What happened?');
var story = (him + " & " + her + " went " + place + " on a date. " + him + ' said "' + heSaid + '". ' + her + ' said "' + sheSaid + '". ' + happenings + ".");
document.write(story);

2 Answers

Hugo Paz
Hugo Paz
15,622 Points

Hi Adam,

You can use a function like this one:

function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

Then you can call it in each prompt:

var him = capitalizeFirstLetter(prompt('What was his name?'));
Chris Shaw
Chris Shaw
26,676 Points

Another way you can implement this to make it even clearer is by assigning the function to the __proto__ of the String object as you can see below.

String.prototype.capitalizeFirstLetter = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

alert('testing'.capitalizeFirstLetter());

Of course this is a little more advanced but it provides more context than a function.

Adam Stonehouse
Adam Stonehouse
4,645 Points

First off, thank you Hugo Paz and secondly Chris thanks for the reply. This is all a little beyond me at the moment, but I will be sure to revisit once I have covered some more JavaScript.

Many thanks to you both!

Adam