Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Errors In My Javascript Code

ReubenO

New Coder
Hi everyone, I need help urgently!

I was charged with resolving the issues with these javascript codes. I am just a starter and therefore, I will need all the help I can get.

I keep getting these error messages:

1. formatTime.js

My First Code:


Code:
// formats the current date/time so that it reads as hh:mm:ss PM/AM
function formatTime(time) {
  hour = time.getHours();
  if (hour>12) {
    hour = hour-12;
    meridies = "PM";
  } else {
    meridies = "AM";
  }
  minute = time.getMinutes();
  if (minute<10) {
    minute = "0"+minute;
  }
  second = time.getSeconds();
  if (second<10) {
    second = "0"+second;
  }
  return hour+":"+minute+":"+second+" "+meridies;
}

The Errors: I Get

'hour' is not defined.
'meridies' is not defined.
'minute' is not defined.
'second' is not defined.

2. times.js

My Code:


Code:
/* global formatTime: true */
/* Please do not remove the comment above. */

// timer to calculate the starting and stopping clicks
$(document).ready(function() {
  $("#start").on('click',function() {
    $("#start").addClass("hidden");
    $("#stop").removeClass("hidden");
    $("#time_ended").addClass("hidden");
    $("#time_started").removeClass("hidden");
    start_time = new Date;
    formatted_time = formatTime(start_time);
  });

  $("#stop").on('click',function() {
    $("#stop").addClass("hidden");
    $("#reset").removeClass("hidden");
    $("#time_started").addClass("hidden");
    $("#time_ended").removeClass("hidden");
    end_time = new Date;
    formatted_end_time = formatTime(end_time);
    $("body").append("<p class='results'>You started at "+formatted_time+".</p>");
    $("body").append("<p class='results'>You finished at "+formatted_end_time+".</p>");
    time_change = end_time-start_time;
    $("body").append("<p class='results'>You counted "+(time_change/1000).tofixed(2)+" seconds.</p>");
    $("body").append("<p class='results'>You are off by "+(time_change/1000-45).tofixed(2)+" seconds.</p>");
  });
});

The Errors I get:

Missing '()' invoking a constructor.
'start_time' is not defined.
'formatted_time' is not defined.
'end_time' is not defined.
'formatted_end_time' is not defined.
'time_change' is not defined.

PLEASE HELP! I am desperate.

Thank you.
 
Hi there,
For the [variable name] is not defined errors, that's because you haven't declared the variables.
Use the let keyword if you plan on changing the value later, or const if the value should always stay the same. For example:
JavaScript:
hour = time.getHours();        /*hour is not defined*/
let hour = time.getHours();    /*hour is now defined and the value can later be changed*/
const hour = time.getHours();  /*hour is also defined, but is a constant so the value cannot be changed.*/
However you only do this once to declare the variable, and to later change the value, you would just do [variable] = [new value]. So in your if condition, where you have hour = hour-12;, you would not use the let keyword because hour has already been defined.

For this error: Missing '()' invoking a constructor., can you give the line number of that error, or which statement that is? This may be where you use new Date, instead of new Date(), but that still works fine and doesn't produce an error for me.
 
Hey There!
The error [variable name] is not defined happens because the variable hasn’t been declared yet. You can fix this by using either let or const depending on your needs:
  • Use let if you plan to change the value later.
  • Use const if the value should stay the same throughout the program.
consider the following code as an example
JavaScript:
hour = time.getHours();         
let hour = time.getHours();     
const hour = time.getHours();
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom