• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 06:29
CEST 12:29
KST 19:29
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Code S Season 1 - RO8 Preview3[ASL21] Ro8 Preview Pt2: Progenitors8Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun13[ASL21] Ro8 Preview Pt1: Inheritors16[ASL21] Ro16 Preview Pt2: All Star10
Community News
Maestros of The Game 2 announcement and schedule !7Weekly Cups (April 27-May 4): Clem takes triple0RSL Revival: Season 5 - Qualifiers and Main Event12Code S Season 1 (2026) - RO12 Results12026 GSL Season 1 Qualifiers25
StarCraft 2
General
Code S Season 1 - RO8 Preview Behind the Blue - Team Liquid History Book Weekly Cups (April 27-May 4): Clem takes triple Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Code S Season 1 (2026) - RO12 Results
Tourneys
Maestros of The Game 2 announcement and schedule ! GSL Code S Season 1 (2026) Sea Duckling Open (Global, Bronze-Diamond) RSL Revival: Season 5 - Qualifiers and Main Event Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players
External Content
Mutation # 524 Death and Taxes The PondCast: SC2 News & Results Mutation # 523 Firewall Mutation # 522 Flip My Base
Brood War
General
Do we have a pimpest plays list? BGH Auto Balance -> http://bghmmr.eu/ (Spoiler) Asl ro8 D winner interview BW General Discussion AI Question
Tourneys
[ASL21] Ro8 Day 4 Escore Tournament StarCraft Season 2 Small VOD Thread 2.0 [BSL22] RO16 Group Stage - 02 - 10 May
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates What's the deal with APM & what's its true value Any training maps people recommend?
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread OutLive 25 (RTS Game) Dawn of War IV
Dota 2
The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
Canadian Politics Mega-thread European Politico-economics QA Mega-thread US Politics Mega-thread The Letting Off Steam Thread Russo-Ukrainian War Thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread McBoner: A hockey love story Formula 1 Discussion
World Cup 2022
Tech Support
streaming software Strange computer issues (software) [G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
How EEG Data Can Predict Gam…
TrAiDoS
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1022 users

The Big Programming Thread - Page 269

Forum Index > General Forum
Post a Reply
Prev 1 267 268 269 270 271 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
FFGenerations
Profile Blog Joined April 2011
7088 Posts
Last Edited: 2013-03-14 14:25:56
March 14 2013 14:25 GMT
#5361
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks
Cool BW Music Vid - youtube.com/watch?v=W54nlqJ-Nx8 ~~~~~ ᕤ OYSTERS ᕤ CLAMS ᕤ AND ᕤ CUCKOLDS ᕤ ~~~~~~ ༼ ᕤ◕◡◕ ༽ᕤ PUNCH HIM ༼ ᕤ◕◡◕ ༽ᕤ
Yoshi-
Profile Joined October 2008
Germany10227 Posts
March 14 2013 14:26 GMT
#5362
if(grade="")

That is not a comparison, it just sets the variable grade to an empty content.
FFGenerations
Profile Blog Joined April 2011
7088 Posts
March 14 2013 14:41 GMT
#5363
Okay thanks, silly error
Cool BW Music Vid - youtube.com/watch?v=W54nlqJ-Nx8 ~~~~~ ᕤ OYSTERS ᕤ CLAMS ᕤ AND ᕤ CUCKOLDS ᕤ ~~~~~~ ༼ ᕤ◕◡◕ ༽ᕤ PUNCH HIM ༼ ᕤ◕◡◕ ༽ᕤ
AmericanUmlaut
Profile Blog Joined November 2010
Germany2594 Posts
Last Edited: 2013-03-14 15:14:07
March 14 2013 15:13 GMT
#5364
On March 14 2013 23:41 FFGenerations wrote:
Okay thanks, silly error

I recommend Yoda notation. Lots of people don't like it, but you can seriously get used to reading it quite quickly, and it makes errors like that impossible:

if("" = grade){
...
}


If you made the same mistake, but always put the constant you're comparing to on the left side of the operator, you'll get an exception instead of the unexpected behavior you were getting, because you can't assign a new value to ""
The frumious Bandersnatch
AmericanUmlaut
Profile Blog Joined November 2010
Germany2594 Posts
Last Edited: 2013-03-14 15:20:09
March 14 2013 15:19 GMT
#5365
On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks

This isn't an answer to your actual question, but I'd like to suggest an improvement in your program:


if("" == grade)
{
alert("You need to enter a grade")
}
else if(grade > -1 && grade < 40)
{
alert("This is below threshold.")
}
else if(grade<50)
{
alert("This is a third class degree.")
}
else if(grade<60)
{
alert("This is a lower second degree.")
}
else if(grade<70)
{
alert("This is an upper second degree.")
}
else if(grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


Your checks like "grade>39" are redundant, because they come after you have already determined that grade is not less than 40 (otherwise you would have run into the previous if).
The frumious Bandersnatch
Frigo
Profile Joined August 2009
Hungary1023 Posts
March 14 2013 15:33 GMT
#5366
On March 14 2013 03:48 ragz_gt wrote:
Show nested quote +
On March 13 2013 20:08 Frigo wrote:
On March 13 2013 07:00 Frigo wrote:
On March 13 2013 02:21 ragz_gt wrote:
Does anyone know if there is a JUnit contrib that allow customizable test execution order?

I remember such a feature in a newer JUnit release. It can at least randomize orders I believe, not sure about a user-defined order though.
Can tell you more details tomorrow when I have access to work mail.


http://www.hascode.com/2012/11/new-features-in-junit-4-11/

Yeah, it is possible.


It is rather limited that it can only sort by method name (and ascend only, can't descend LOL). But I guess I can name stuff by levels (1000s, 2000s etc) and should work

Kind of silly though

Yeah, they implemented it in a very stupid way, can't even extend it.
http://www.fimfiction.net/user/Treasure_Chest
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-03-14 17:45:08
March 14 2013 17:32 GMT
#5367
On March 15 2013 00:19 AmericanUmlaut wrote:
Show nested quote +
On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks

This isn't an answer to your actual question, but I'd like to suggest an improvement in your program:
+ Show Spoiler +


if("" == grade)
{
alert("You need to enter a grade")
}
else if(grade > -1 && grade < 40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade<60)
{
alert("This is a lower second degree.")
}
else if(grade<70)
{
alert("This is an upper second degree.")
}
else if(grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


Your checks like "grade>39" are redundant, because they come after you have already determined that grade is not less than 40 (otherwise you would have run into the previous if).


Isn't it better if the code is only:

+ Show Spoiler +

if(grade>=40&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade >= 50 && grade<60)
{
alert("This is a lower second degree.")
}
else if(grade >= 60 && grade<70)
{
alert("This is an upper second degree.")
}
else if(grade >= 70 && grade<=100)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


It looks a bit more clear with <= 100, and you also skip the -1 & 40 check and the one for empty grade. They are covered in the last else.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
Last Edited: 2013-03-14 18:04:25
March 14 2013 18:02 GMT
#5368
On March 15 2013 02:32 darkness wrote:
Show nested quote +
On March 15 2013 00:19 AmericanUmlaut wrote:
On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks

This isn't an answer to your actual question, but I'd like to suggest an improvement in your program:
+ Show Spoiler +


if("" == grade)
{
alert("You need to enter a grade")
}
else if(grade > -1 && grade < 40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade<60)
{
alert("This is a lower second degree.")
}
else if(grade<70)
{
alert("This is an upper second degree.")
}
else if(grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


Your checks like "grade>39" are redundant, because they come after you have already determined that grade is not less than 40 (otherwise you would have run into the previous if).


Isn't it better if the code is only:

+ Show Spoiler +

if(grade>=40&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade >= 50 && grade<60)
{
alert("This is a lower second degree.")
}
else if(grade >= 60 && grade<70)
{
alert("This is an upper second degree.")
}
else if(grade >= 70 && grade<=100)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


It looks a bit more clear with <= 100, and you also skip the -1 & 40 check and the one for empty grade. They are covered in the last else.


I actually prefer data based code for those situations. My javascript is rusty so the syntax might not be 100% correct or optimal:


var boundaries = new Array(
new Array(39, function(){}),
new Array(49, function(){alert("This is a third class degree.")}),
new Array(59, function(){alert("This is a lower second class degree.")}),
new Array(69, function(){alert("This is a upper second class degree.")}),
new Array(100, function(){alert("This is a first class degree.")})
);

// foreach would be better but don't remember if it was native JS
for (i = 0; i < boundaries.length; ++i)
{
if (grade <= boundaries[i][0])
{
boundaries[i][1]();
break;
}
}
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-03-14 18:21:53
March 14 2013 18:19 GMT
#5369
^ what's the value of boundaries.length? 100 or number of arrays (5 in this case)? If the former, then I'm not sure if this would be any faster/better because of numerous iterations and you also use memory to create arrays.

Edit: It should be the former like other languages, but I'm not familiar with JavaScript anyway.
alwinuz
Profile Joined September 2011
Netherlands77 Posts
March 14 2013 18:53 GMT
#5370
I don't think speed matters, it's more a question of what is more readable and maintainable code.
Write easy code first, optimize for speed later (and measure first!).
But is this more readable than the original code? Not sure... personal preference I think.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
March 14 2013 19:17 GMT
#5371
On March 15 2013 03:53 alwinuz wrote:
I don't think speed matters, it's more a question of what is more readable and maintainable code.
Write easy code first, optimize for speed later (and measure first!).
But is this more readable than the original code? Not sure... personal preference I think.


I don't really want to start a big huff over nothing, but there is a misconception I don't want spread. The idea that an easily maintainable design is a trade off with with code speed is absurd. The better the design, the smaller the code base, more maintainable, readable, and faster it will be. If you find that your code is running slow, then optimizing it will only cause problems in your understanding of the code. This is often why people rewrite programs from scratch, because they tried to optimize something, realized they did the design wrong, and start over.

Optimization should be done out the gate, so that your design pattern will reflect the quickest way to solve the problem, and be easier to understand because of it.

Any sufficiently advanced technology is indistinguishable from magic
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
March 14 2013 19:23 GMT
#5372
On March 15 2013 04:17 RoyGBiv_13 wrote:
Show nested quote +
On March 15 2013 03:53 alwinuz wrote:
I don't think speed matters, it's more a question of what is more readable and maintainable code.
Write easy code first, optimize for speed later (and measure first!).
But is this more readable than the original code? Not sure... personal preference I think.


I don't really want to start a big huff over nothing, but there is a misconception I don't want spread. The idea that an easily maintainable design is a trade off with with code speed is absurd. The better the design, the smaller the code base, more maintainable, readable, and faster it will be. If you find that your code is running slow, then optimizing it will only cause problems in your understanding of the code. This is often why people rewrite programs from scratch, because they tried to optimize something, realized they did the design wrong, and start over.

Optimization should be done out the gate, so that your design pattern will reflect the quickest way to solve the problem, and be easier to understand because of it.


Depends on what kind of optimization you're talking about. You're basically talking about having a good model of the program before you make it, which indeed benefits both maintainability and speed, but I don't think most people would call that optimization, that's more along the lines of "either you know what you're doing before you go in, or you fuck it up and rewrite it, learning something in the process". It's not like you write a program, are really proud of it, test the speed and rewrite it from scratch because a completely different design would be faster.

So I would say the idea "make it first, then optimize where needed" makes sense because the optimization you're talking about isn't something you can do ahead of time since you don't know what it is, you already designed the program wrong from the start if you need to do it.
FFGenerations
Profile Blog Joined April 2011
7088 Posts
March 14 2013 19:50 GMT
#5373
well you could just put more thought into what you're doing; my example was just a super fast messing around thing to get something working in javascript, however if i thought for 10 seconds i could have written it better like demonstrated, or even better yet
Cool BW Music Vid - youtube.com/watch?v=W54nlqJ-Nx8 ~~~~~ ᕤ OYSTERS ᕤ CLAMS ᕤ AND ᕤ CUCKOLDS ᕤ ~~~~~~ ༼ ᕤ◕◡◕ ༽ᕤ PUNCH HIM ༼ ᕤ◕◡◕ ༽ᕤ
AmericanUmlaut
Profile Blog Joined November 2010
Germany2594 Posts
March 14 2013 21:09 GMT
#5374
On March 15 2013 02:32 darkness wrote:
Show nested quote +
On March 15 2013 00:19 AmericanUmlaut wrote:
On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks

This isn't an answer to your actual question, but I'd like to suggest an improvement in your program:
+ Show Spoiler +


if("" == grade)
{
alert("You need to enter a grade")
}
else if(grade > -1 && grade < 40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade<60)
{
alert("This is a lower second degree.")
}
else if(grade<70)
{
alert("This is an upper second degree.")
}
else if(grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


Your checks like "grade>39" are redundant, because they come after you have already determined that grade is not less than 40 (otherwise you would have run into the previous if).


Isn't it better if the code is only:

+ Show Spoiler +

if(grade>=40&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade >= 50 && grade<60)
{
alert("This is a lower second degree.")
}
else if(grade >= 60 && grade<70)
{
alert("This is an upper second degree.")
}
else if(grade >= 70 && grade<=100)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}


It looks a bit more clear with <= 100, and you also skip the -1 & 40 check and the one for empty grade. They are covered in the last else.

You changed his logic - the original function generated a different message for values 0-40 than for values outside the range 0-100. If I enter "30" into your function, I get "Please enter a grade from 0 to 100", that's silly.
The frumious Bandersnatch
misirlou
Profile Joined June 2010
Portugal3300 Posts
March 14 2013 21:23 GMT
#5375
On March 15 2013 03:19 darkness wrote:
^ what's the value of boundaries.length? 100 or number of arrays (5 in this case)? If the former, then I'm not sure if this would be any faster/better because of numerous iterations and you also use memory to create arrays.

Edit: It should be the former like other languages, but I'm not familiar with JavaScript anyway.


The later, like EVERY other language. Boundaries is an array with 5 elements (which are other arrays of 2 elements each but they could be ints objects or arrays of arrays, thats not relevant), so the length of boundaries is 5.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2013-03-14 21:42:45
March 14 2013 21:34 GMT
#5376
On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks



Your code seems to be able to compare integers and strings without regard for the data type.

I think after you've validated the grade is a number, you have to apply a parseInt(grade), then you can perform all these if..else checks. That's what I think I did when I did JS some years ago.

I think it parses your string grade into ASCII, which is causing your unexpected behavior.
There is no one like you in the universe.
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
March 14 2013 21:37 GMT
#5377
On March 15 2013 06:34 Blisse wrote:

Show nested quote +
On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks



Your code seems to be able to compare integers and strings without regard for the data type.

I think you have to apply a parseInt(grade) after you've validated the grade is a number, before you can perform all these checks. That's what I think I did when I did JS some years ago.

There's no problem comparing strings and numbers in javascript. Try something like:
alert("20" > 5) and you'll get true.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2013-03-14 22:42:13
March 14 2013 21:44 GMT
#5378
On March 15 2013 06:37 Tobberoth wrote:
Show nested quote +
On March 15 2013 06:34 Blisse wrote:

On March 14 2013 23:25 FFGenerations wrote:
Using javascript & html, i have a button on a page and a text box. User inputs a grade number into the text box and presses the button. An alert pops up based on the input, using a simple if statement, telling the user if the entered grade is a merit, pass etc.

+ Show Spoiler +

if(grade!="")
{
if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}
else
{
alert("Please enter a grade from 0 to 100.")
}
}


The question is, why do i need to lay out everything within a second if statement?
The first time i wrote it, i had similar to the following:

+ Show Spoiler +


if(grade="")
{
alert("You need to enter a grade")
}
else if(grade>-1&&grade<40)
{
alert("This is below threshold.")
}
else if(grade>39&&grade<50)
{
alert("This is a third class degree.")
}
else if(grade>49&&grade<60)
{
alert("This is a lower second degree.")
}
else if(grade>59&&grade<70)
{
alert("This is an upper second degree.")
}
else if(grade>69&&grade<101)
{
alert("This is a first class degree.")
}
else
{
alert("Please enter a grade from 0 to 100.")
}



however, this kept displaying "This is below threshold." instead of running the if(grade="") part.
My tutor said it might be something to do with the if statements switching from string ("") to int (grade>-1&&grade<40) which is somehow messing it up.

Many thanks



Your code seems to be able to compare integers and strings without regard for the data type.

I think you have to apply a parseInt(grade) after you've validated the grade is a number, before you can perform all these checks. That's what I think I did when I did JS some years ago.

There's no problem comparing strings and numbers in javascript. Try something like:
alert("20" > 5) and you'll get true.


Because "20" as an ASCII value should be greater than 5, at least it would make sense if it were.

"4" > 5 doesn't seem like it would return true, but it probably does here. Again, not a JS person.

And anyways, I don't believe you're suggesting one should compare strings with integers like that. Best practice should always be string with string and int with int.
There is no one like you in the universe.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
March 14 2013 21:49 GMT
#5379
On March 15 2013 03:53 alwinuz wrote:
I don't think speed matters, it's more a question of what is more readable and maintainable code.
Write easy code first, optimize for speed later (and measure first!).
But is this more readable than the original code? Not sure... personal preference I think.


I think it's definitely more readable and maintainable.

There is just one place you have to look at to know where every level begins and ends, you don't have to go through each "else if" to find all the values. If you need to adjust it you instantly know where you have to do it, no need to search through potentially dozens of cases. If you have to add a case, no need to search through all branches to know where it belongs, you can see it in the short list.

Sure, in this case it's a small amount of branches but i had a similar situation with price groups where certain video lengths resulted in different prices and there were twice as many potential values and the previous "if/else if" stuff was far too long. After i changed it to something similar to my code above (in PHP), it was so easy that i was able to copy-paste the list into an email to the product manager and he understood it easily.

I could probably also have written it like this, which is easier to the untrained eye:

var boundaries = new Array();
boundaries["39"] = "";
boundaries["49"] = "third level";
...
Orome
Profile Blog Joined June 2004
Switzerland11984 Posts
March 15 2013 02:18 GMT
#5380
Hey guys, small C++ question that I hope someone can help me with. Assuming we have a std::stack called someStack consisting of SomeClass objects, why does this sort of code work fine?

SomeClass *next = someStack.top();
stack.pop();
next.printWhatever();


Since next is a pointer variable pointing to someStack.top(), shouldn't it point to a null object once that object is destroyed through pop?
On a purely personal note, I'd like to show Yellow the beauty of infinitely repeating Starcraft 2 bunkers. -Boxer
Prev 1 267 268 269 270 271 1032 Next
Please log in or register to reply.
Live Events Refresh
The PondCast
10:00
Episode 92
CranKy Ducklings39
LiquipediaDiscussion
Escore
10:00
Week 6
escodisco1176
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech142
StarCraft: Brood War
Calm 3134
Sea 1913
Horang2 372
EffOrt 278
actioN 276
BeSt 231
Hyuk 218
Killer 202
Light 169
Mini 156
[ Show more ]
Zeus 143
Soma 126
Soulkey 115
Stork 114
ggaemo 100
Dewaltoss 82
ZerO 74
ToSsGirL 70
Pusan 66
Hm[arnc] 60
Mong 56
Backho 50
hero 49
Hyun 45
Rush 39
910 39
Sharp 37
Free 31
Liquid`Ret 29
Shine 22
sorry 21
Shinee 20
Bale 18
soO 17
scan(afreeca) 14
[sc1f]eonzerg 13
Barracks 13
ajuk12(nOOB) 11
Sacsri 10
yabsab 10
GoRush 8
Terrorterran 4
Dota 2
monkeys_forever169
Counter-Strike
zeus693
allub229
edward86
Other Games
gofns28587
singsing1456
B2W.Neo348
DeMusliM300
KnowMe130
Lowko126
Mew2King90
NeuroSwarm71
ZerO(Twitch)9
Organizations
Other Games
gamesdonequick1143
StarCraft: Brood War
lovetv 10
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 18 non-featured ]
StarCraft 2
• CranKy Ducklings SOOP51
• LUISG 35
• StrangeGG 16
• Adnapsc2 2
• LaughNgamezSOOP
• sooper7s
• Migwel
• IndyKCrew
• Kozan
• intothetv
• AfreecaTV YouTube
StarCraft: Brood War
• iopq 4
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• TFBlade886
• Stunt529
Other Games
• WagamamaTV224
Upcoming Events
WardiTV Invitational
31m
Zoun vs Ryung
Lambo vs ShoWTimE
Big Brain Bouts
5h 31m
Fjant vs Bly
Serral vs Shameless
OSC
11h 31m
The PiG Daily
12h 31m
Maru vs Rogue
TBD vs Classic
herO vs Solar
ByuN vs Solar
Replay Cast
13h 31m
CranKy Ducklings
23h 31m
RSL Revival
23h 31m
SHIN vs Bunny
ByuN vs Shameless
WardiTV Invitational
1d
Krystianer vs TriGGeR
Cure vs Rogue
SC Evo League
1d 2h
uThermal 2v2 Circuit
1d 4h
[ Show More ]
BSL
1d 8h
Artosis vs TerrOr
spx vs StRyKeR
Replay Cast
1d 13h
Sparkling Tuna Cup
1d 23h
RSL Revival
1d 23h
Cure vs Zoun
Clem vs Lambo
WardiTV Invitational
2 days
BSL
2 days
Dewalt vs DragOn
Aether vs Jimin
GSL
2 days
Afreeca Starleague
2 days
Soma vs Leta
Wardi Open
3 days
Monday Night Weeklies
3 days
OSC
3 days
CranKy Ducklings
3 days
Afreeca Starleague
3 days
Light vs Flash
Replay Cast
4 days
Replay Cast
5 days
The PondCast
5 days
Replay Cast
6 days
RSL Revival
6 days
Liquipedia Results

Completed

Proleague 2026-05-05
WardiTV TLMC #16
Nations Cup 2026

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
KCM Race Survival 2026 Season 2
Acropolis #4
Escore Tournament S2: W6
SCTL 2026 Spring
RSL Revival: Season 5
2026 GSL S1
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2

Upcoming

KK 2v2 League Season 1
BSL 22 Non-Korean Championship
YSL S3
Escore Tournament S2: W7
Escore Tournament S2: W8
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
2026 GSL S2
BLAST Bounty Summer 2026: Closed Qualifier
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.