Any idea why my JS function isn't working?

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Function Idea
Click For Summary

Discussion Overview

The discussion revolves around a JavaScript function designed to convert time in the format hh:mm:ss into seconds. Participants are troubleshooting issues with the function's output and exploring ways to improve its structure and efficiency.

Discussion Character

  • Technical explanation
  • Debugging
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant reports that the function returns "2" for the input "00:02:29" and seeks help in identifying the issue.
  • Another participant suggests using the radix parameter in the parseInt function to avoid potential misinterpretation of values like "08" or "09".
  • Some participants propose restructuring the code to consolidate error checking into fewer statements or a separate function.
  • There are suggestions to debug the function by adding alert calls or using browser debugging tools to inspect variable values.
  • Multiple participants point out that the substring parameters in the parseInt calls may be incorrect and need adjustment.
  • Concerns are raised about the implications of not specifying the radix in parseInt, particularly with inputs starting with "0".
  • One participant emphasizes the importance of checking for errors either all at once or incrementally as the function processes input.
  • Another participant expresses uncertainty about the expected behavior of parseInt with different inputs and suggests testing with "09:09:09".
  • There is a discussion about the clarity and presentation of the code, with differing opinions on whether to check for errors before or during the parsing process.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to error checking and code structure. There are multiple competing views regarding the use of parseInt and the handling of input validation.

Contextual Notes

Some participants note that the function's behavior may depend on the specific implementation of parseInt in different browsers, particularly regarding the handling of octal and decimal interpretations.

  • #31
I second harborsparrow's recommendation of "JavaScript: The Good Parts". For reference, I also have David Flanagan's "JavaScript: The Definitive Guide", 6th Ed.

Jamin2112, I don't know if you took me up on my suggestion to use the F12 debugging tools (available in pretty much any browser). Debugging your code using these capabilities would provide answers to many of the questions you've asked.

There is also the timeworn method of using print statements to display the values of variables at certain locations in your code. You can do this by adding alert(...) statements in your code.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
12
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K