sonya1m Posted March 22, 2013 Share Posted March 22, 2013 I am doing a uni assignment and I had to as for the input as a word not as a number and then convert the word to an integer. I am having trouble understanding how to do this. Can someone give me an example and explain the process to me please. We were given the months to be 0 to 11 and use only the first three letter and to convert to lower case. Then to change to integer. Any help and advise would be welcomed. Sonya var months = 'janfebmaraprmayjunjulaugsepoctnovdec'; var birthMonth = prompt( 'Enter the name of the month of birth' ); var pos = months.indexOf( birthMonth.substring( 0, 3 ).toLowerCase() ); alert( 'Month number: ' + ( pos / 3 ) ); Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/ Share on other sites More sharing options...
requinix Posted March 22, 2013 Share Posted March 22, 2013 So you mean that code doesn't work? Looks like it does. Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420254 Share on other sites More sharing options...
sonya1m Posted March 22, 2013 Author Share Posted March 22, 2013 (edited) So you mean that code doesn't work? Looks like it does. This is my whole script. It is meant to output ( working on the date 1 jan 2000) interllectual: -0.5 emotional: -0.6 physical 0.1 My results are: interllectual; 0.9 emotional: 0.9 physical 0.1 So I am doing something wrong, and my teacher said arguments passed to Date object should be Number values so thats why I thought I wasn't changing the month to a number. However if it looks like it is working then there is something else that I am not doing correctly but just don't know. <script type="text/javascript"> var months = 'janfebmaraprmayjunjulaugsepoctnovdec'; var PI = 3.14159265358979323846; var msPerDay = 1000*60*60*24; var interllectualCycle = 33; var emotionalCycle = 28; var physicalCycle = 23; var msYear = 365; //User enters year of birth year, month, day var birthYear = parseInt(prompt( 'Enter year of birth as a 4 digit integer')); // alert('Your year of birth is '+ birthYear); var birthMonth = prompt( 'Enter the name of the month of birth' ); //gets the months position var pos = months.indexOf( birthMonth.substring( (0, 3 /3) /3 ).toLowerCase() ); alert( 'Month number: ' + pos ); var birthDay = parseInt (prompt( 'Enter day of birth as an integer ')); var yearString = birthYear; var monthNumber = pos; var dayString = birthDay; //puts users birthdate into string var userBirthday = new Date(yearString , monthNumber, dayString); // alert ('userBirthday' + userBirthday); //date object created for current date var currentDate = new Date(); // alert ('' +currentDate) // getting users age in milliseconds var msSinceBirth = currentDate.getTime()- userBirthday.getTime() ; //alert ('' +msSinceBirth); //getting user age in days var userMsDays = Math.floor(msSinceBirth / msPerDay); // document.write ('' + userMsDays); // getting user age in years //var userAge = Math.floor(userMsDays / msYear); // alert ('' + userAge); document.write('You have been alive'+'' + userMsDays +''+ 'days'); document.write('<h1>"Your biorhythm energy levels are :-"</h1>'); document.write('<ul>'); var bioEnergy = ( (userMsDays * PI * 2) / interllectualCycle); document.write( '<li><font color="green"> intellectual:</font></li>' + [Math.sin(bioEnergy).toFixed(1)] ); var bioEmot = ((userMsDays * PI * 2) / emotionalCycle); document.write('<li><font color="red">emotional:</font></li>' + [Math.sin(bioEnergy).toFixed(1)]); var bioPhy = ((userMsDays * PI * 2) / physicalCycle); document.write('<li><font color="blue">physical: </font></li>' + [Math.sin(bioPhy).toFixed(1)]); document.write('</ul>'); </script> Edited March 22, 2013 by sonya1m Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420264 Share on other sites More sharing options...
requinix Posted March 22, 2013 Share Posted March 22, 2013 Having never seen the syntax before I'm surprised that var pos = months.indexOf( birthMonth.substring( (0, 3 /3) /3 ).toLowerCase() );is valid, but apparently it is. Anyway, looks like you repeated yourself a bit too much in that call to .substring(). Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420266 Share on other sites More sharing options...
sonya1m Posted March 22, 2013 Author Share Posted March 22, 2013 Having never seen the syntax before I'm surprised that var pos = months.indexOf( birthMonth.substring( (0, 3 /3) /3 ).toLowerCase() );is valid, but apparently it is. Anyway, looks like you repeated yourself a bit too much in that call to .substring(). Thanks for that, yes had to many /3's, but still gives out wrong output lol...... Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420269 Share on other sites More sharing options...
requinix Posted March 22, 2013 Share Posted March 22, 2013 What formula or specification are you working from? Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420349 Share on other sites More sharing options...
Solution salathe Posted March 22, 2013 Solution Share Posted March 22, 2013 var pos = months.indexOf( birthMonth.substring( (0, 3 /3) /3 ).toLowerCase() ); This is trying, and failing, to get the position of the month name within the months string. There are two issues: first is that you're doing something weird to try and get the first three characters of birthMonth; second is that you're correctly trying to divide the string index by three (to go from string index to month number) but in completely the wrong place. Getting the first three characters of the birth month is easy enough with the correct syntax. birthMonth.substring(0, 3)Dividing the index to get a month number should happen only after you've gotten the index; not somewhere in the middle.var pos = months.indexOf(birthMonth.substring(0, 3).toLowerCase()) / 3;Finally, it might be worthwhile checking to see whether the user entered a valid month name. Otherwise, the pos is going to be rubbish. Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420362 Share on other sites More sharing options...
salathe Posted March 22, 2013 Share Posted March 22, 2013 Having never seen the syntax before I'm surprised that var pos = months.indexOf( birthMonth.substring( (0, 3 /3) /3 ).toLowerCase() );is valid, but apparently it is. Anyway, looks like you repeated yourself a bit too much in that call to .substring(). The comma operator is a very esoteric operator, rarely seen out in the wild. I'm not surprised that you're surprised that it is valid. Its use is discouraged. Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420365 Share on other sites More sharing options...
.josh Posted March 22, 2013 Share Posted March 22, 2013 is there any reason why you can't simplify it by putting the months in an array instead of doing all that indexOf, substring and math stuff? Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420387 Share on other sites More sharing options...
sonya1m Posted March 22, 2013 Author Share Posted March 22, 2013 This is trying, and failing, to get the position of the month name within the months string. There are two issues: first is that you're doing something weird to try and get the first three characters of birthMonth; second is that you're correctly trying to divide the string index by three (to go from string index to month number) but in completely the wrong place. Getting the first three characters of the birth month is easy enough with the correct syntax. birthMonth.substring(0, 3)Dividing the index to get a month number should happen only after you've gotten the index; not somewhere in the middle. var pos = months.indexOf(birthMonth.substring(0, 3).toLowerCase()) / 3;Finally, it might be worthwhile checking to see whether the user entered a valid month name. Otherwise, the pos is going to be rubbish. Thank you for that, I had come up with using another var to do the / 3 calculation, so thank you, I will take that step out and do it the way you have put it. In the assignment we are assuming that they will put in right info, as this is only my third week of javascript. Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420388 Share on other sites More sharing options...
sonya1m Posted March 22, 2013 Author Share Posted March 22, 2013 is there any reason why you can't simplify it by putting the months in an array instead of doing all that indexOf, substring and math stuff? Its just the way we have to do it in the assignment. Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420389 Share on other sites More sharing options...
sonya1m Posted March 22, 2013 Author Share Posted March 22, 2013 The comma operator is a very esoteric operator, rarely seen out in the wild. I'm not surprised that you're surprised that it is valid. Its use is discouraged. Can you show me a better way to writing this code to get the same result. I am new to javascript and to learn the right way straight off would be great. Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420391 Share on other sites More sharing options...
sonya1m Posted March 22, 2013 Author Share Posted March 22, 2013 (edited) The comma operator is a very esoteric operator, rarely seen out in the wild. I'm not surprised that you're surprised that it is valid. Its use is discouraged. The only other thing I am having trouble with is trying to get each output on one line. ex intellectual: 0.8 this is the result I am trying to get. I have tried +""+ but obviously that is not the right way of doing it. My out put isintellectual: 0.8 Edited March 22, 2013 by sonya1m Quote Link to comment https://forums.phpfreaks.com/topic/275995-how-to-convert-a-word-to-a-integer/#findComment-1420407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.