Jump to content

Help with getting code to work in javascript


Go to solution Solved by nogray,

Recommended Posts

<script>
  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 =  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 ).toLowerCase() );

       // alert( 'Month number: ' + (  pos / 3 ) );

  var birthDay = 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);
   


  var bioEnergy = (userMsDays * PI / 33)/2;
  document.write('intellectual:' + Math.sin(bioEnergy).toFixed(1));
   
   var bioEmot = (userMsDays * PI / 28)/2;
  document.write('emotional:' + Math.sin(BioEmot).toFixed(1));
   
   var bioPhy = (userMsDays * PI /23)/2;
  document.write('physical: ' + Math.sin(bioPhy).toFixed(1));
   



   


  

   document.write('You have been alive' + userMsDays + 'days');
   document.write('<h1>"Your biorhythm energy levels are :-"</h1>');
   document.write('<ul>');
   document.write('intellectual:' + [Math.sin(bioEnergy).toFixed(1)]);
   document.write('emotional:' + [Math.sin(BioEmot).toFixed(1)]);
   document.write('physical:' + Math.sin(BioPhy).toFixed(1));
   document.write('</ul>');







</script>

 

 

First assignment from uni and have run into trouble.

 

Task was to create a program that would calculate users biorythm energy levels, intellectual, emotional and physical. User had to put in birthyear, birth month (in text) and birth day. 

 

The first part I have done and is working. I am having trouble with the last few lines in the code:

I have to multiply the user's age in days by two times PI and divide by the lenght in days of the cycles 

 

 var interllectualCycle = 33;

  var emotionalCycle = 28;

  var physicalCycle = 23;   

 

giving me the intellectual, emotional and physical biorythm numbers. I have tried to do this   var bioEnergy = (userMsDays * PI / 33)/2;    document.write('intellectual:' + Math.sin(bioEnergy).toFixed(1));and i know i have done it wrong in the first one  but it seems to work. and gives me a interllectual number of -0.5 wihich is correct. The other 2 don't output anything at all. Their output numbers should be -0.6 and 0.1. Going on the birthdate of 1 Jan 2000.

 

Then moving down to the last bit of the code it works until the same line as above, then no more output. I also have to have unordered list with intellectual in green and so on.... I know how to do colours in html but they don't seem to want to work that well with my code.

 

Any advise would be much appreciated .

 

Thank you 

 

Sonya

 

I have to multiply the user's age in days by two times PI and divide by the lenght in days of the cycles

 

You have your math incorrect then. This:

 

 

var bioEnergy = (userMsDays * PI / 33)/2;

Should be this:

 

var bioEnergy = (userMsDays * PI * 2) / 33;

 

(according to your description)

As a side note, I'm disappointed to see that they are teaching this style of javascripting in your class - the JavaScript you are using is quite outdated. After you've finished your course, I'd suggest buying an up-to-date book on JS and learning how to do it properly. What you have will work, but it's not how things are done anymore. But since I don't want to confuse you, I'll leave you with what you've got. I just wanted to give a pointer for the future.

You have your math incorrect then. This:

 

 

var bioEnergy = (userMsDays * PI / 33)/2;

Should be this:

 

 

var bioEnergy = (userMsDays * PI * 2) / 33;

 

(according to your description)

When i do it like that it give the number 0.9 and the correct answer is -0.5.  Also after it displays  this no other document.write works. I have had comments about the prompt and month input as being outdated. Anyway will keep trying to get it to work and hopefully we are just taught this way to start off with. Thanks for your help.

Can someone help me please. have i missed some steps in the last bit of coding? It is not working, plus when i try and add the font and list html requirements, it doesn't work at all.....

 

Have been working on this part for two days now and am no closer to working it out. Plus my calculations are not working how they should. I have checked and my code up until the last 10 or so lines work. I just feel that maybe I need to add a lot more code to get the results that are being asked of my.

 

Thank you 

 

Sonya

<script>
  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 ).toLowerCase() );

       // alert( 'Month number: ' + (  pos / 3 ) );

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

  var bioEnergy =  (userMsDays * PI * 2) / 33;
 // document.write('intellectual:' + Math.sin(bioEnergy).toFixed(1));
   
   var bioEmot = (userMsDays * PI * 2) / 28;
 // document.write('emotional:' + Math.sin(BioEmot).toFixed(1));
   
   var bioPhy = (userMsDays * PI * 2) / 23;
 // document.write('physical: ' + Math.sin(bioPhy).toFixed(1));
   
	


   


  

   document.write('You have been alive' + userMsDays + 'days');
   document.write('<h1>"Your biorhythm energy levels are :-"</h1>');
   document.write('<ul>');
   document.write('intellectual:' + [Math.sin(bioEnergy).toFixed(1)]);
   document.write('emotional:' + [Math.sin(BioEmot).toFixed(1)]);
   document.write('physical:' + Math.sin(BioPhy).toFixed(1));
   document.write('</ul>');

 
 
 



</script>
  • Solution

If you use IE, you can always hit the F12 button to see the developer tools which will point you to the error in your code. JavaScript variables are case sensitive, so BioEmot and BioPhy in your document.write are not defined (should be bioEmot, bioPhy).

 

You might also need some basic HTML touch up in your list, each list item should be enclosed in a <li></li>

Edited by nogray

If you use IE, you can always hit the F12 button to see the developer tools which will point you to the error in your code. JavaScript variables are case sensitive, so BioEmot and BioPhy in your document.write are not defined (should be bioEmot, bioPhy).

 

You might also need some basic HTML touch up in your list, each list item should be enclosed in a <li></li>

Thank you for that, yes I see my mistake, amazing how spelling can keep you searching for days, I guess that comes with experience. Can I just quickly get your advise on my html. I am trying to put the <li> </li> and change colours of output, but when I put the html in it won't work. Not sure how to insert them in with whats already there.

 

Thanks

 

Sonya

 

If you use IE, you can always hit the F12 button to see the developer tools which will point you to the error in your code. JavaScript variables are case sensitive, so BioEmot and BioPhy in your document.write are not defined (should be bioEmot, bioPhy).

 

You might also need some basic HTML touch up in your list, each list item should be enclosed in a <li><

 

Thank you for that, yes I see my mistake, amazing how spelling can keep you searching for days, I guess that comes with experience. Can I just quickly get your advise on my html. I am trying to put the <li> </li> and change colours of output, but when I put the html in it won't work. Not sure how to insert them in with whats already there.

 

Thanks

 

Sonya

I have fixed the problem with the html now except for the fact I can't seem to work out how to keep the out put on one line. The below code prints like:

intellectual :

0.9 

instead of intellectual:  0.9

 

document.write( '<li><font color="green"> intellectual:</font></li>'  + [Math.sin(bioEnergy).toFixed(1)] );

First assignment from uni and have run into trouble.

 

Task was to create a program that would calculate users biorythm energy levels, intellectual, emotional and physical. User had to put in birthyear, birth month (in text) and birth day. 

 

The first part I have done and is working. I am having trouble with the last few lines in the code:

I have to multiply the user's age in days by two times PI  ( not sure how to calculate this)and divide by the length in days of the cycles. I just don't seem to beable to get it to work. Could someone please check my script and point out any coding that may be the problem please? Going on the date 01 Jan 2000 the out put should be -0.5, -0.6, 0.1.

 

 var interllectualCycle = 33;

  var emotionalCycle = 28;

  var physicalCycle = 23;   

<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 birthYear =  parseInt(prompt( 'Enter year of birth as a 4 digit integer'));

  var birthMonth = prompt( 'Enter the name of the month of birth' );
  
//gets the months position

  var pos = months.indexOf( birthMonth.substring(0, 3) 
   .toLowerCase() );
  var posMonth = pos / 3;
  
         //alert( 'Month number: ' + pos );

// Change month to number
	  
  var numberInString = posMonth;

  var numberMonth = new Number(numberInString);

        // alert(numberMonth);


  var birthDay = parseInt (prompt( 'Enter day of birth as an integer ')); 

//puts users birthdate into string

  var userBirthday = new Date(birthYear , numberMonth, birthDay);
       //alert ('' + 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); 
		

   var bioEnergy = ( userMsDays * (2 * PI ) / interllectualCycle);
   var bioEmot = (userMsDays * (2 * PI) / emotionalCycle);
   var bioPhy = (userMsDays *(2 * PI)/ physicalCycle );
   
//Biorhythm energy levels output   
  document.write('You have been alive' + userMsDays + 'days');
  document.write('<h1>"Your biorhythm energy levels are :-"</h1>');
  document.write('<ul>');  
  document.write( '<li><font color="green"> intellectual:</font></li>'  + [Math.sin(bioEnergy).toFixed(1)] ); 
  document.write('<li><font color="red">emotional:</font></li>' +'' +  [Math.sin(bioEmot).toFixed(1)]); 
  document.write('<li><font color="blue">physical: </font></li>' + [Math.sin(bioPhy).toFixed(1)]);
  document.write('</ul>');
	
 

</script>

 

 

Any advise would be much appreciated .

 

Thank you 

 

Sonya

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.