Jump to content

How to convert a word to a integer


sonya1m
Go to solution Solved by salathe,

Recommended Posts

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 ) );

 

 

Link to comment
Share on other sites

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 by sonya1m
Link to comment
Share on other sites

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().
Link to comment
Share on other sites

 

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......

Link to comment
Share on other sites

  • Solution

 

 

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

 

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. 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 is
  • intellectual:

          0.8

Edited by sonya1m
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.