Unit 5 Lesson 9



Hi, there. My students are working on Computer Science Principles Unit 5 Lesson 9. On puzzle 13, they need to write code so if a person enters “Saturday” or “Sunday” something happens. Problem: if a user enters “saturday” or “sunday” or “SUNDAY” or “SATURDAY” the words are not recognized as the same. I used day = day.toUpperCase to solve the problem, but I wanted to use .toTitleCase instead, which does not work. This is an option that is not listed in the Code.org Tool Documentation. How can I change a text input by a user into a TitleCase?


@rossiai - First I like that you are being thorough but I think you are going well beyond the scope of this exercise. Unless the student has a lot of programming skill this is not an easy thing to do. I didn’t see a predefined .toTitleCase in JavaScript when i searched for it. What I see is a bunch of functions that others have written to do it. I could be wrong.


Thank you for your answer. I come from a different subject and I am learning all this with my students. I ran into the problem, so I wanted to know how to solve it.
Here I am sharing the solution that I found, and also a much better solution that my student Brandon G, suggested.
Converting text input by user into titleCase


Your solution is great. The example solution just assumes the user will input the days in title case.
Brandon’s solution is nearly perfect. It will not work is someone enters SUNDAY or sUnday, etc. Here’s a solution that would work for all cases (note I only added the one line to the toTitleCase function!)

function toTitleCase(str) {
return str.charAt(0).toUpperCase() + str.substring(1);

var day = toTitleCase(prompt(“Enter a day of the week.”));
var age = promptNum(“Enter your age.”);
write("Day is: " + day);
write("Age is: " + age);
if ((day == “Saturday” || day == “Sunday” ) && (age >= 13 && age <= 19)) {
write(“Sleep in”);
} else if (day == “Monday” || day == “Tuesday” || day == “Wednesday” || day == “Thursday” || day == “Friday” ) {
write(“You need to wake up”);
} else {
write(“You did not enter a day”);
//write your code here


Wow, fantastic. I will share with my students!