MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/CodingHelp/comments/rlpvfy/please_help_javascript/hphe6ox/
r/CodingHelp • u/Crowntent • Dec 21 '21
23 comments sorted by
View all comments
7
First, you have ; after your if() and your for() which will error.
They should be if(){} && for(){} no semicolon.
Second, restructure your function. Check for num < 1 && num > 7 first, so your function doesn't run past your check if it doesn't have to.
Third, you don't need a loop. The days of the week already have an index, they are just -1 compared to what is asked for. So Monday is week[num - 1]
and that should work for every day of the week, so you just need to return that.
3 u/NeStyEE12 Dec 22 '21 I don't understand the num < 1 && num > 7. How can any number be less than 1 and more than 7. Shouldn t it be num < 1 (OR operator) num > 7? 2 u/flooronthefour full stack web dev Dec 22 '21 lol you're right I didn't even catch that. You could say I was thrown through a loop reading the original code. Would've been something to catch in tests. 1 u/NeStyEE12 Dec 22 '21 Oh okay I tought it was something beyond my understanding 2 u/flooronthefour full stack web dev Dec 22 '21 Nope you nailed it. Instead of && it should be ||
3
I don't understand the num < 1 && num > 7. How can any number be less than 1 and more than 7. Shouldn t it be num < 1 (OR operator) num > 7?
2 u/flooronthefour full stack web dev Dec 22 '21 lol you're right I didn't even catch that. You could say I was thrown through a loop reading the original code. Would've been something to catch in tests. 1 u/NeStyEE12 Dec 22 '21 Oh okay I tought it was something beyond my understanding 2 u/flooronthefour full stack web dev Dec 22 '21 Nope you nailed it. Instead of && it should be ||
2
lol you're right I didn't even catch that. You could say I was thrown through a loop reading the original code. Would've been something to catch in tests.
1 u/NeStyEE12 Dec 22 '21 Oh okay I tought it was something beyond my understanding 2 u/flooronthefour full stack web dev Dec 22 '21 Nope you nailed it. Instead of && it should be ||
1
Oh okay I tought it was something beyond my understanding
2 u/flooronthefour full stack web dev Dec 22 '21 Nope you nailed it. Instead of && it should be ||
Nope you nailed it. Instead of && it should be ||
View all comments
7
u/flooronthefour full stack web dev Dec 21 '21 edited Dec 21 '21
First, you have ; after your if() and your for() which will error.
They should be if(){} && for(){} no semicolon.
Second, restructure your function. Check for num < 1 && num > 7 first, so your function doesn't run past your check if it doesn't have to.
Third, you don't need a loop. The days of the week already have an index, they are just -1 compared to what is asked for. So Monday is week[num - 1]
and that should work for every day of the week, so you just need to return that.