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
ALEXANDER HUNTER
Courses Plus Student 30 PointsPlease check/ comment on my code
This is the first thing I've written so just after some comments/ feedback
<html>
<head>
<script>
MONTHS = 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'; //months
document.write ('<h1>Introduce Yourself</h1><hr />');
var userName = ''; //create username variable
userName = prompt('Enter your First Name','Alex'); //prompt user for name
var dateString = prompt('Enter your birth date in the form dd mmm yyyy','26 APR 1988'); //get users date of birth
var substrings = dateString.split(' ');
var day = substrings[0];
var month = substrings[1];
var year = substrings[2];
var monthNumber = month.indexOf(MONTHS);
if (monthNumber >= 0); {
}
var poem = ["Sunday's child is fair and wise", "Monday's child is fair of face", "Tuesday's child is full of grace", "Wednesday's child is full of woe", "Thursday's child has far to go", "Friday's child is loving and giving", "Saturday's child works hard for a living"];
var daysArray = new Array ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var birthDate = new Date(year, monthNumber/3, day);
var dayNumber = (birthDate.getDay());
var dayName = daysArray[dayNumber];
var dayPoem = poem[dayNumber];
document.writeln ('<p><ul><li><h2>My name is: <font color="red"> '+ userName +' </font></li><li><h2> I was born on a '+ dayName +': <font color="blue"><em> '+dayPoem+' </em> </font></h2></li></ul></p>');
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];
var visualBar = 30*visual
var auralBar = 30*aural
var readBar = 30*read
var kinestheticBar = 30*kinesthetic
document.write ('<h2><font color="green">My Vark Scores</font></h2>');
document.writeln ('<img src="chart_key.png"/><br />');
document.writeln('<img src="bar_red.png" width='+visualBar+' height="25"/>('+visual+')<br />');
document.writeln('<img src="bar_yellow.png" width='+auralBar+' height="25"/>('+aural+')<br />');
document.writeln('<img src="bar_blue.png" width='+readBar+' height="25"/>('+read+')<br />');
document.writeln('<img src="bar_green.png" width='+kinestheticBar+' height="25"/>('+kinesthetic+')<br />');{
}
</script>
</head>
<body>
</body>
<html>
1 Answer
Marcus Parsons
15,719 PointsFrom what you've pasted, the code looks good except for a few parts:
- Your username variable is correctly initialized, but the next line is not only redundant but wrong itself. There's no need to even do two lines to initialize the variable in Javascript, and you cannot use the word "variable" as a keyword before a variable. It has to be "var" (if you want to use var at all, which you don't have to although using var and not using it changes scope of the variable respectively) but once you've initialized it, you don't put var again, because it just initializes it again, which makes it redundant.
var userName = ''; //create username
variable userName = prompt('Enter your First Name','Alex'); //prompt user for name
Should be:
var userName = prompt('Enter your First Name','Alex'); //prompt user for name
- Your if-statement is not actually doing anything at all, and you never put a semi-colon at the end of the parenthesis of an if-statement. I'm certain you meant for the code below it to be a part of the code block, but in order for that to happen the ending } must come after all of the code within the block. Here's the revised version
if (monthNumber >= 0) {
var poem = ["Sunday's child is fair and wise", "Monday's child is fair of face", "Tuesday's child is full of grace", "Wednesday's child is full of woe", "Thursday's child has far to go", "Friday's child is loving and giving", "Saturday's child works hard for a living"];
var daysArray = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var birthDate = new Date(year, monthNumber/3, day);
var dayNumber = birthDate.getDay();
var dayName = daysArray[dayNumber];
var dayPoem = poem[dayNumber];
document.writeln('My name is: '+ userName +' I was born on a '+ dayName +': '+dayPoem+' ');
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];
var visualBar = 30*Number(visual);
var auralBar = 30*Number(aural);
var readBar = 30*Number(read);
var kinestheticBar = 30*Number(kinesthetic);
document.write('<h2><font color="green">My Vark Scores</font></h2>');
document.writeln('<img src="chart_key.png"/><br />');
document.writeln('<img src="bar_red.png" width='+visualBar+' height="25"/>('+visual+')<br />');
document.writeln('<img src="bar_yellow.png" width='+auralBar+' height="25"/>('+aural+')<br />');
document.writeln('<img src="bar_blue.png" width='+readBar+' height="25"/>('+read+')<br />');
document.writeln('<img src="bar_green.png" width='+kinestheticBar+' height="25"/>('+kinesthetic+')<br />');
}
There are some redundancies, spacing issues, and other errors happening beyond that. I used Number() to convert the strings received visual, aural, read, and kinesthetic into numbers.
Also, I would recommend doing some error checking. The user may
It was a little bit difficult to follow because there is hardly any spacing in between lines of code. When you post code, please wrap it within the code tags that you can find on the Markdown Cheatsheet. They are 3 tick marks at beginning, press Enter and paste code, then press Enter at the end, and put 3 more tick marks. (on the same button as the ~).
I hope that helps! :)
ALEXANDER HUNTER
Courses Plus Student 30 PointsThanks very much! Only thing I'm confused about is dayNumber, I put it in to use get.date to get the day of the year, how is it doing this in the revised version?
Marcus Parsons
15,719 PointsApologies! I actually did not notice the division that you did. But that is easily remedied, and I revised my own copy to reflect yours. I see what you did now! :) Any questions you have don't hesitate to ask. That's what we're here for. :)
Marcus Parsons
15,719 PointsHere is the complete revised program, for easy copying and pasting my friend.
<html>
<head>
<script>
MONTHS = 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'; //months
document.write('<h1>Introduce Yourself</h1><hr />');
var userName = prompt('Enter your First Name','Alex'); //prompt user for name
var dateString = prompt('Enter your birth date in the form dd mmm yyyy','26 APR 1988'); //get users date of birth
var substrings = dateString.split(' ');
var day = substrings[0];
var month = substrings[1];
var year = substrings[2];
var monthNumber = month.indexOf(MONTHS);
if (monthNumber >= 0) {
var poem = ["Sunday's child is fair and wise", "Monday's child is fair of face", "Tuesday's child is full of grace", "Wednesday's child is full of woe", "Thursday's child has far to go", "Friday's child is loving and giving", "Saturday's child works hard for a living"];
var daysArray = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var birthDate = new Date(year, monthNumber/3, day);
var dayNumber = birthDate.getDay();
var dayName = daysArray[dayNumber];
var dayPoem = poem[dayNumber];
document.writeln('My name is: '+ userName +' I was born on a '+ dayName +': '+dayPoem+' ');
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];
var visualBar = 30*Number(visual);
var auralBar = 30*Number(aural);
var readBar = 30*Number(read);
var kinestheticBar = 30*Number(kinesthetic);
document.write('<h2><font color="green">My Vark Scores</font></h2>');
document.writeln('<img src="chart_key.png"/><br />');
document.writeln('<img src="bar_red.png" width='+visualBar+' height="25"/>('+visual+')<br />');
document.writeln('<img src="bar_yellow.png" width='+auralBar+' height="25"/>('+aural+')<br />');
document.writeln('<img src="bar_blue.png" width='+readBar+' height="25"/>('+read+')<br />');
document.writeln('<img src="bar_green.png" width='+kinestheticBar+' height="25"/>('+kinesthetic+')<br />');
}
</script>
</head>
<body>
</body>
<html>
Marcus Parsons
15,719 PointsYou're doing great, Alexander! This is a great starting point!
Nick Ocampo
15,661 PointsNick Ocampo
15,661 PointsHey Alexander, I added code markdown to your post so that your code is formatted as code. For future reference, you can click on the link below the text field when you're writing the post that says markdown cheatsheet and you will find some tips on how to format your code like that.