Reputation:
i'm new to programming and have tried to program if-else logic to create arrays etc..
I want to use a points variable to decide which points-interval the variable falls within and then make the array with funfacts for that interval and return a random funfact from this array.
For instance I have the milestones 1000, 2500, etc. If the userScorePoints is over 2500, I want the method to return a random funfact from the array that contains funfact about that number, up to the point that userScorePoints has reached the next milestone, which is 5000.
The problem with the code I have written is that it only returns a random funfact from the first if, so I only get funfacts from the number 1000, even though I should have got funfacts from the number 2500 since my points now is over 2603..
Can someone please help me with this..?
Here is my code:
function getFunfact(userScorePoints) {
var array = new Array();
if (1000 <= userScorePoints < 2500) {
var funfact1000 = new Array();
funfact1000[0] = "funfacts about the number 1000";
funfact1000[1] = "...";
funfact1000[2] = "...";
funfact1000[3] = "...";
funfact1000[4] = "...";
funfact1000[5] = "...";
array = funfact1000;
} else if (2500 <= userScorePoints < 5000) {
var funfact2500 = new Array();
funfact2500[0] = "funfacts about the number 2500";
funfact2500[1] = "...";
funfact2500[2] = "...";
funfact2500[3] = "...";
funfact2500[4] = "...";
funfact2500[5] = "...";
array = funfact2500;
} else if (5000 <= userScorePoints < 10000) {
var funfact5000 = new Array();
funfact5000[0] = "funfacts about the number 5000";
funfact5000[1] = "...";
funfact5000[2] = "...";
funfact5000[3] = "...";
funfact5000[4] = "..."
funfact5000[5] = "...";
array = funfact5000;
} else if (10000 <= userScorePoints < 20000) {
var funfact10000 = new Array();
funfact10000[0] = "funfacts about the number 10.000";
funfact10000[1] = "...";
funfact10000[2] = "...";
funfact10000[3] = "...";
funfact10000[4] = "...";
funfact10000[5] = "...";
array = funfact10000;
} else if (20000 <= userScorePoints < 30000) {
var funfact20000 = new Array();
funfact20000[0] = "funfacts about the number 20.000";
funfact20000[1] = "...";
funfact20000[2] = "...";
funfact20000[3] = "...";
funfact20000[4] = "...";
funfact20000[5] = "...";
array = funfact20000;
} else if (30000 <= userScorePoints < 50000) {
//etc.
} else {}
return array[getRandom(6)]; //this method returns a random element, this one works.
Upvotes: 3
Views: 150
Reputation: 2725
Tushar is right. Use:
function getFunfact(userScorePoints) {
var array = new Array();
if ((1000 <= userScorePoints) && (userScorePoints < 2500)) {
var funfact1000 = new Array();
funfact1000[0] = "funfacts about the number 1000";
funfact1000[1] = "...";
funfact1000[2] = "...";
funfact1000[3] = "...";
funfact1000[4] = "...";
funfact1000[5] = "...";
array = funfact1000;
} else if ((2500 <= userScorePoints) && (userScorePoints < 5000)) {
var funfact2500 = new Array();
funfact2500[0] = "funfacts about the number 2500";
funfact2500[1] = "...";
funfact2500[2] = "...";
funfact2500[3] = "...";
funfact2500[4] = "...";
funfact2500[5] = "...";
array = funfact2500;
} else if ((5000 <= userScorePoints) && (userScorePoints < 10000)) {
var funfact5000 = new Array();
funfact5000[0] = "funfacts about the number 5000";
funfact5000[1] = "...";
funfact5000[2] = "...";
funfact5000[3] = "...";
funfact5000[4] = "..."
funfact5000[5] = "...";
array = funfact5000;
} else if ((10000 <= userScorePoints) && (userScorePoints < 20000)) {
var funfact10000 = new Array();
funfact10000[0] = "funfacts about the number 10.000";
funfact10000[1] = "...";
funfact10000[2] = "...";
funfact10000[3] = "...";
funfact10000[4] = "...";
funfact10000[5] = "...";
array = funfact10000;
} else if ((20000 <= userScorePoints) && (userScorePoints < 30000)) {
var funfact20000 = new Array();
funfact20000[0] = "funfacts about the number 20.000";
funfact20000[1] = "...";
funfact20000[2] = "...";
funfact20000[3] = "...";
funfact20000[4] = "...";
funfact20000[5] = "...";
array = funfact20000;
} else if ((30000 <= userScorePoints) && (userScorePoints < 50000)) {
//etc.
} else {}
return array[getRandom(6)];
}
Upvotes: 0
Reputation: 2941
You'll need to use something like this:
if (1000 <= userScorePoints && userScorePoints < 2500)
if (2500 <= userScorePoints && userScorePoints < 5000)
etc...
The syntax you used is not going to give you the result you want, as Javascript will parse it like this:
if((1000 <= userScorePoints)/*either true(1) or false(0)*/ < 2500)/* Always true,
since both 0 and
1 are < 2500 */
Upvotes: 1
Reputation: 10992
Syntax is wrong for what you actually want-
This is the correct syntax -
if(5000 <= userScorePoints && userScorePoints < 10000)
Use '&&' for multiple logical comparisons.
I will also explain what does the interpreter understand when you write your code which is -
if(5000 <= userScorePoints < 10000)
basically doe the first comparison 5000 <= userScorePoints
. The result will either be a true
or false
which is equivalent to 1
or 0
respectively when converted into a number.
So in the next step you compare either 0 < 10000
if the result of the previous comparison is false or 1 < 10000
if the result is true. In both cases the values are less than 10000 and that's why the condition is always true.
Hope this clears up your doubt. Happy coding!
Upvotes: 4
Reputation: 27823
Javascript doesn't chain comparisons like that.
if (a < b < c)
will always be true for c
larger than 1
*.
The solution is to replace them with if (a<b && b < c)
a<b<c
is equivalent to (a<b)<c
due to Javascript operator precedence rules. a<b
will return true
or false
which when compared to a number are type-cast to 1
or 0
. The conclusion is that all of your comparisons actually compare c
with 0
or 1
.Upvotes: 2
Reputation: 7848
Programming is not mathematics. Your condintions should be in the form:
if (1000 <= userScorePoints && userScorePoints < 2500)
Upvotes: 3
Reputation: 781370
You can't chain relational comparisons like that. You have to write:
if (1000 <= userScorePoints && userScorePoints < 2500) {
...
}
What you wrote was parsed as if you'd written:
if ((1000 <= userScorePoints) < 2500) {
...
}
The comparison in parentheses evaluates to either 0 or 1, which is always less than 2500.
Upvotes: 5