New answers tagged

2

Your algorithm seems good, but the while loop, and lecture_hour and lecture_minute variables make your code a lot more complicated. If we KISS then a simple algorithm is to just remove () from Lectures and iterate through it, since it is sorted. The first lecture that is after the current time is the lecture we want. This is nice and simple: import datetime ...


4

Better is relative! Lots of ways to write that code. Though I personally prefer terse and precise code like other answers have highlighted, (here is my take on that): const minuteConverter = time => [time.split(':')] .map(([hour, minute]) => +(+hour + +minute / 60).toFixed(2))[0] The pluses could get a little hectic so here is a more descriptive/...


3

Use + instead of Number() on ES6. Use const instead of let or var if the value for this variable not going to change. Use a deconstructing assignment to create variables from the array let [h,m] = time.split(/[.:]/). This will accept 10:30 or 10.30, also in case the time is .30 will add 0 for hours variable. const timeStringToFloatMm = time => { let [h,...


15

Use const instead of let to declare variables if the value doesn't change. You are executing time.split(':') twice. A short method to convert a string to a number is the unary +. JavaScript has the toFixed() method to format a number to a fixed number of digits: function minuteConverter(time) { const [h, m] = time.split(':'); const value = +h + +m /...


6

Simpler technique Per answers to this identical question from six years ago on stack overflow the formula for converting the minutes doesn’t need to be so complex. Many of the answers there use parseInt() to parse numbers from the strings but the unary plus operator + can be used instead for faster operation and simpler syntax (refer to answers to parseInt ...


0

Use doctests You have written lots of comments, especially docstrings, which is great practice. def seconds_to_text(seconds): """Return the user-friendly version of the time duration specified by seconds. Outputs should resemble: "3 hours and 30 minutes" "20 minutes" "1 minute and 30 ...


2

From a short review; Naming could be better for when you look back at the code; var d could be const now var e could be const hour var f could be let shippingLabel toLocaleString does not do what you want with time zones As a beginner, I would not go for ternary to keep things ultra simple So my counter proposal would be const now = new Date(); const ...


4

Your code has a bug: d.getHours() will return a plain number. Calling toLocaleString on that number calls Number.prototype.toLocaleString, which: returns a string with a language-sensitive representation of this number. It does not do anything with timezones; the second parameter does not recognize a timeZone property. For example, where I am now, ....


2

First, here is my recommended replacement for your script (inspired by this SO post) and some test cases: Code: (Demo) function secondsToTime($seconds) { $dtF = new DateTime('@0'); $dtT = new DateTime("@$seconds"); $diff = $dtF->diff($dtT); $units = [ 'y' => 'year', 'm' => 'month', 'd' => 'day', ...


Top 50 recent answers are included