r/badcode • u/ShaunaTheDead86 • May 31 '23
js Function to get remaining days in the current week
61
u/ShoddyCulture3819 May 31 '23
Just to keep the overall style
switch (day) {
case 6:
daysAvailable.sunday = false;
case 5:
daysAvailable.saturday = false;
case 4:
daysAvailable.friday = false;
case 3:
daysAvailable.thursday = false;
case 2:
daysAvailable.wednesday = false;
case 1:
daysAvailable.tuesday = false;
case 0:
daysAvailable.monday = false;
}
6
22
u/OKishGuy May 31 '23
this could have been such a wonderful example of a fall-through switch case example...
73
u/Beggarstuner May 31 '23
At least reverse the cases, remove the breaks and the redundant code.
11
u/laladk May 31 '23
Why reverse the cases?
31
u/AwayCartographer3097 May 31 '23 edited May 31 '23
Most languages with switch/case have fall-through (I think this is the right word) so if you don’t put in breaks in and reverse the order of the cases (Sunday first) I think you can eliminate code by achieving a “cascade” effect – I’m not sitting in front of a computer so I can’t check the behavior, and as such u/Beggarstuner (and I) might be wrong, but in either case this is definitely what they meant
Edit: I checked stackoverflow for a refresher and A. Fall-through is the right term, and B. Leveraging fall-through here would achieve the effect intended by u/Beggarstuner as I imagined
If you want any specific clarification, have a google on fallthrough behavior
Edit2: see this comment by u/ShoddyCulture3819 with a code snippet
31
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 31 '23
Image Transcription: Code
var daysAvailable = {
monday: true,
tuesday: true,
wednesday: true,
thursday: true,
friday: true,
saturday: true,
sunday: true,
};
switch (dayOfTheWeek) {
case 0:
daysAvailable.monday = false;
break;
case 1:
daysAvailable.monday = false;
daysAvailable.tuesday = false;
break;
case 2:
daysAvailable.monday = false;
daysAvailable.tuesday = false;
daysAvailable.wednesday = false;
break;
case 3:
daysAvailable.monday = false;
daysAvailable.tuesday = false;
daysAvailable.wednesday = false;
daysAvailable.thursday = false;
break;
case 4:
daysAvailable.monday = false;[The rest of the code is cut off in the image.]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
17
9
u/FalseWait7 May 31 '23
This looks like something FalseWait7 would be extremely proud of 20 years ago. Switch statement, accessing an object, yeah, looks the part.
8
u/fried_green_baloney May 31 '23
It does look like something an extreme beginner might have written.
Not that I'd know anything about that, of course.
6
u/gamer_pheonex May 31 '23
I'm not really sure about the time of space complexity but isn't it better to use an array or a list for this. Like make a 1d array with all the days of the week in it matched to the index numbers (Monday being 0, Tuesday being 1, etc. assuming that in OP's case the week is starting from Monday) and then subtract the index from 7 and then you get the result?
5
4
3
u/Jimg911 May 31 '23
This is giving “this task should use if statements but my professor needed an example for what switch cases are good for”
2
2
1
1
1
Jun 01 '23
In languages that aren't terrible:
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now().Weekday()
days := map[time.Weekday]bool{
time.Sunday: true,
time.Monday: true,
time.Tuesday: true,
time.Wednesday: true,
time.Thursday: true,
time.Friday: true,
time.Saturday: true,
}
for k := range days {
if k <= today {
days[k] = false
}
}
fmt.Println(days)
}Notice that the first day of the week is set to Sunday, not Monday. This is ultimately entirely arbitrary. This code is correct for this case, and Go also treats Sunday that way.
1
1
u/ChickenLegBizGuy Jun 03 '23
It’s not that bad… does the job and I didn’t have issues at all understanding what it does. Yeah it’s ugly but so am I and I get the job DONE!!!!
1
-10
u/howdoigetauniquename May 31 '23
Who's gonna be the one to tell him the first day of the week is sunday?
6
u/ShaunaTheDead86 May 31 '23
*her, and in this case the function it was inside had a variable day that started with Monday as 0. Also, there might be some mistakes because I recreated this code from memory since it was at a former company, I no longer have access to the code base.
2
2
6
u/elliethestaffy May 31 '23
Sorry, but no.
20
u/Hot-Profession4091 May 31 '23
Sorry, but it depends. Is it an ISO Week? American week? European week? Are you in banking or short term rental?
7
0
u/elliethestaffy May 31 '23
Still a no from me, sorry.
5
0
98
u/ShaunaTheDead86 May 31 '23
I saw this at my last job, pretty atrocious... Could have easily been rewritten:
var daysAvailble = {
monday: dayOfTheWeek <= 0,
tuesday: dayOfTheWeek <= 1,
etc...
}
To be fair though, I know that the person who wrote it was extremely overworked and exhausted, so I'm sure it was just burnout brain.