-1

A web page that I made for checking the amount of possible 3 digit numbers using only the digits 1 - 6 in order of least to greatest doesn't load. I'm guessing it's from a mistake in the while loop I put in the code below. I've tried making something that returns the same value as a string of the object {a:4, b:5, c:6}, and it didn't work. I tried using the string 'a:4, b:5, c:6', and it didn't work.

    var n = 0;
    var o = {a:1, b:2, c:3};
    var i = [o.a, o.b, o.c];
    var s = i.sort(function(a, b) {
        return a - b;
    });
    while (o != {a:4, b:5, c:6}) {
        o.c++;
        i = [o.a, o.b, o.c];
        if (o.c = 7) {
            o.c = 1;
            o.b++;
        }
        if (o.b = 7) {
            o.b = 1;
            o.a++
        }
        if (i == s && o.a != o.b && o.a != o.c) {
            n++;
            document.getElementById('a').innerHTML = n;
        }
    }

Also, the question someone suggested is different from my question. I'm asking why the page isn't loading and how to fix the while loop, the other person suggested why two empty objects aren't the same.

Meep alien
  • 13
  • 7
  • 2
    The while loops condition will *never* be false. Try this in your console: `{} === {}` An empty object will not equal the empty object. Object's use referential equality, not structural equality. –  Feb 12 '20 at 22:31
  • 3
    Does this answer your question? [Why are two identical objects not equal to each other?](https://stackoverflow.com/questions/11704971/why-are-two-identical-objects-not-equal-to-each-other) – Heretic Monkey Feb 12 '20 at 22:32
  • This happened To me before. And it was because I did not close the document after writing in it. So just try to close it at the end and see if that work – MadeInDreams Feb 12 '20 at 22:37
  • 1
    Use a [comparison](https://stackoverflow.com/a/52323412/567595) function if you really need to compare two objects. It looks like it may be more appropriate for you to use an array anyway (and you would then need to [compare the arrays](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript)) – Stuart Feb 12 '20 at 22:44

1 Answers1

1

Also, the question someone suggested is different from my question. I'm asking why the page isn't loading and how to fix the while loop, the other person suggested why two empty objects aren't the same.

It's really the same. In your code, you continue the loop when o != {a:4, b:5, c:6}. The thing is that this condition CANNOT be false. So the loop is doomed to go on forever.

As per your question "why the page doesn't load" the answer is simple: JavaScript code running locks the rendering of the page. So the page cannot render while your code is executing. Your code doesn't stops, so you will never see anything. We could have a lengthy discussion on how to delay the calculation after render, but it could be as simple as:

function theCodeYouPosted() {
}

// this will delay execution of your code for 100 milliseconds,
// so the page could render.
setTimeout(function() {
  theCodeYouPosted();
}, 100);

This is one of many, many possible workarounds, but it's not a solution for your main problem, which is that your loop doesn't exits.

You need a condition that truly checks the equality of the 3 values you are searching. Please, please read carefully the comments: I know you are eager to "solve your problem", but that is not the right approach. You need to understand your problem. Then your problem will solve by itself.

You need to compare property by property, something like:

while(!(o.a === 4 && o.b === 5 && o.c === 6)) {
  ...
}

but I think there are multiple problems in your code. You should try your debugger and try to understand how the code you wrote works.

The easiest way is to press F12 in your browser and insert a debugger statement just before the code you want to understand.

Bon Voyage!

Alberto Chiesa
  • 7,022
  • 2
  • 26
  • 53