This is my code. It's fairly simple...I do not understand why it's giving me a result of 2, 1, 3 no matter what I do and not 1, 2 , 3 when I try to sort 3, 2 and 1. Did I make a mistake somewhere in my logic or the code?
function sort (a, b, c) {
if(a < b && b < c) {
document.write(a, b , c);
} else if (a < b && c < b) {
document.write(a, c, b);
} else if (b < a && a < c) {
document.write(b, a, c);
} else if(b < a && c < a) {
document.write(b, c, a);
} else if(c < a && a < b) {
document.write(c, a, b);
} else {
document.write(c, b , a);
}
}
sort(3,2,1);
This is my code. It's fairly simple...I do not understand why it's giving me a result of 2, 1, 3 no matter what I do and not 1, 2 , 3 when I try to sort 3, 2 and 1. Did I make a mistake somewhere in my logic or the code?
function sort (a, b, c) {
if(a < b && b < c) {
document.write(a, b , c);
} else if (a < b && c < b) {
document.write(a, c, b);
} else if (b < a && a < c) {
document.write(b, a, c);
} else if(b < a && c < a) {
document.write(b, c, a);
} else if(c < a && a < b) {
document.write(c, a, b);
} else {
document.write(c, b , a);
}
}
sort(3,2,1);
Share
Improve this question
asked Jul 4, 2018 at 18:11
zuccinnizuccinni
632 gold badges2 silver badges10 bronze badges
5 Answers
Reset to default 4In the following condition:
else if (a < b && c < b) {
document.write(a, c, b);
}
What if c is less than a? You are just checking that whether a < b and c < b, but your are not checking which number is greater between a and c.
Example :
var a = 9, b = 10, c = 8;
According to the above condition output will be:
output : 9,8,10
But correct output should be:
Correct output : 8,9,10
Similarly for condition :
else if(b < a && c < a) {
document.write(b, c, a);
}
You are doing the exact same thing as above. That is why in case:
var a = 3, var b = 2, var c = 1
Your output is ing out to be : 2,1,3
because you are not paring b with c and you simply print b,c,a
which leads to wrong result.
What you should do is :
else if(b < a && c < a) {
if(b < c){
document.write(b, c, a);
} else{
document.write(c, b, a);
}
}
Here is an other way to write your sort function manipulating if/else statements
with a,b,c you can permute them (3! possibilities) you just write them somewhere
a,b,c
a,c,b
b,a,c,
b,c,a
c,a,b
c,b,a
you can then just test any of those arrangements...
function sort(a,b,c){
if a<=b && b<=c
print a,b,c
else if a<=c && c<=b
print a,c,b
else if b<=a && a<=c
print b,a,c
...
}
Another approach is to pass an array of numbers, and use the sort() method:
const sorted = [3,2,1].sort((a,b) => a - b);
console.log(sorted);
Advantages of sort: it will work with any number of elements, and you don't need to make all the if's manually
You may want to just use the JavaScript Array .sort()
method which would be much more concise.
Example:
function sort (x) {
document.write(x.sort());
}
sort([3,2,1]);
or even just:
document.write([3,2,1].sort());
More information on sort()
: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
else if(b < a && c < a) {
if(b < c){
document.write(b, c, a);
} else{
document.write(c, b, a);
}
}