# Failure to complete conditional statements

**URL:** <https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772>\
**Category:** Uncategorized\
**Created:** [March 25, 2023, 12:52am UTC](https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772 "2023-03-25T00:52:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willwrtr](https://avatars.discourse-cdn.com/v4/letter/w/ad7895/32.png) [@Willwrtr](https://forum.kirupa.com/u/Willwrtr)\
**Post date:** [March 25, 2023, 12:52am UTC](https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772/1 "2023-03-25T00:52:34Z")

</div>

Right after a Do While routine, I have the following. However, it seems to ignore this:

```
		if (coverOne = 1){
				context.fillRect(230, 370, 100, 50);
			}
			 else if (coverOne = 2) {
				context.fillRect(230, 390, 100, 50);
			}
			
			else if (coverOne = 3) {
				context.fillRect(230, 410, 100, 50);
			}

```

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [March 25, 2023, 1:18am UTC](https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772/2 "2023-03-25T01:18:31Z")

</div>

`if (coverOne = 1){` should be `if (coverOne == 1){` or `if (coverOne === 1){`  
Notice that the one that is wrong only has one equal and the other two have 2 or 3.  
One equal sign will assign a value to a variable, whilst `==` and `===` are used to comapare two values. When you assign a value to a variable that statement will return the value you assigned to the variable. So in `if (coverOne = 1){` the `coverOne=1` will assign 1 to `coverOne` and return 1 which is a true value and so `if (coverOne = 1){` will always be true.  
To compare values use `==` or `===`. With two equal signs you get the um, forgot the correct term, but fuzzy comparison where things are converted to true, false values. So 1,“”,{}, etc are considered true, whereas 0, null, undefined, etc are considered false. Three equal signs wont convert anything before doing the comparison so 1 wouldnt equal {} like it would with 2 equal signs. When your just starting out its best to always use 3 to avoid problems with getting the conversion wrong, some people cant remember whats what and get unexpected results. Plus, 3 equal signs can be faster than two which has to do type conversion first, especially in Chrome.

[Assignment (=)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment)  
[Equality (==)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality)  
[Strict equality (===)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality)

> **[Equality Comparisons (v15)](https://jsperf.app/tizolu/15)**
>
> Equality Comparisons (v15) - Online Javascript Benchmark

---

<div class="post-metadata">

**Author:** ![Willwrtr](https://avatars.discourse-cdn.com/v4/letter/w/ad7895/32.png) [@Willwrtr](https://forum.kirupa.com/u/Willwrtr)\
**Post date:** [March 25, 2023, 5:41pm UTC](https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772/3 "2023-03-25T17:41:33Z")

</div>

Thanks. I’ll give that a try. I was aware of == but didn’t think it would make a difference.

---

<div class="post-metadata">

**Author:** ![Willwrtr](https://avatars.discourse-cdn.com/v4/letter/w/ad7895/32.png) [@Willwrtr](https://forum.kirupa.com/u/Willwrtr)\
**Post date:** [March 25, 2023, 6:20pm UTC](https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772/4 "2023-03-25T18:20:12Z")

</div>

Thanks, but that didn’t work. I added a draw rectangular box to see if it would get to that section and it still doesn’t. I tried to add the code here but it wouldn’t accept it, so here’s the link to where it can be found under the update link on this page.  
[Sagebrush Tidbits, geometry section - update link](https://sagebrushtidbits.com/geometry.html)

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [March 26, 2023, 4:16am UTC](https://forum.kirupa.com/t/failure-to-complete-conditional-statements/657772/5 "2023-03-26T04:16:05Z")

</div>

If you ran that in a browser and looked at the Console in the Web Developer Tools you would have seen an error like this…

> Uncaught ReferenceError: context is not defined

If you checked the line number it gave you it would be this…

```javascript
	        // Adding the below draw to see if it gets to this.
It was this line ->	context.fillRect(230, 370, 100, 50);
			// It doesn't complete this, so something is missing in the above script
			// The purpose of the below conditional statement is to cover over one of the three answers
			// with a rectangle.

```

So the execution of the script would have stopped there and never got to the lines with the comparisons. And if you removed that line then it will fail on the comparions as it will say `coverOne` is not defined. The reasons you are getting these errors is because you defined `context` and `coverOne` in the scope of the function assigned to `windows.onload`.  
How and where a variable is defined will affect where else in the code that variable is available. To understand these concepts better look at [MDN - Scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope).  
So to fix these problems all you have to do is move that code to inside the `window.onload` function.  
Here is a working demo for you to look at… [Demo](https://jsbin.com/yeqelayile/edit?html,css,js,output)
