# Coding and Debugging with circuit playground

**URL:** <https://forum.code.org/t/coding-and-debugging-with-circuit-playground/38955>\
**Category:** Coding and Debugging Help\
**Tags:** csd\
**Created:** [January 29, 2024, 1:30am UTC](https://forum.code.org/t/coding-and-debugging-with-circuit-playground/38955 "2024-01-29T01:30:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnarsiman](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bnarsiman/32/6759_2.png) [@bnarsiman](https://forum.code.org/u/bnarsiman)\
**Post date:** [January 29, 2024, 1:30am UTC](https://forum.code.org/t/coding-and-debugging-with-circuit-playground/38955/1 "2024-01-29T01:30:23Z")

</div>

Re Circuit Playground - Lesson 4 - Lock /Unlock App

I have an advanced student who is using code /input for this activity - his code is pasted below. There are no coding errors, however the code is not running how he would like it to. Even if the two arrays are equal it never returns as true.

var pass = ;  
var key = [“1”,“2”];  
var locked = true;

console.log(“input:”+ pass);

onBoardEvent(buttonL,“down”,function(){  
if(pass.length \> 3 && locked == true){  
console.clear();  
pass = ;  
appendItem(pass, “1”);  
console.log(“input:”+ pass);  
} else {  
console.clear();  
appendItem(pass, “1”);  
console.log(“input:”+ pass);  
}  
});  
onBoardEvent(buttonR,“down”,function(){  
if(pass.length \> 3 && locked == true){  
console.clear();  
pass = ;  
appendItem(pass, “2”);  
console.log(“input:”+ pass);  
} else {  
console.clear();  
appendItem(pass, “2”);  
console.log(“input:”+ pass);  
}  
});  
onBoardEvent(toggleSwitch,“change”,function(){  
console.clear();  
console.log(“input:”+ pass);  
console.log(“attempt…”);  
if(pass == key){  
locked = false;  
setProperty(“screen1”,“background-color”,“green”);  
console.log(“unlocked!!!”);  
} else {

```
console.log("failed.");
pass = [];

```

}  
});

Any help greatly appreciated.  
Thank you.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/dan/32/5979_2.png) [@Dan](https://forum.code.org/u/Dan)\
**Post date:** [February 1, 2024, 4:10pm UTC](https://forum.code.org/t/coding-and-debugging-with-circuit-playground/38955/2 "2024-02-01T16:10:16Z")

</div>

Hi there! I suspect the culprit is the way the student is making the comparison:

> if(pass == key){

I suspect what you and your student _hope_ is happening is that Javascript is comparing each value in the array and letting you know if all the values are the same. The reality isn’t the same because of how arrays are represented in Javascript as Objects (which is an advanced topic not covered in CSD).

Instead, one solution is to acutally _do this yourself_: compare each element of the arrays and check if they’re the same. Here’s some algorithmic psuedocode for how that could look:

> If `pass` and `key` have differnet lengths:  
> &nbsp;&nbsp; False  
> //If I made it here: I know they have the same length, so I can use a loop  
> for each element in `pass`:  
> &nbsp; &nbsp; if pass[i] is not equal to key[i]:  
> &nbsp; &nbsp; &nbsp; &nbsp; False  
> //If I made it here: the arrays are the same length and all the values are the same  
> True

There are also lots of resources online on this topic (here’s one for example: [Comparing Arrays in JavaScript – How to Compare 2 Arrays in JS](https://www.freecodecamp.org/news/how-to-compare-arrays-in-javascript/)). You could encourage your student to explore answers online too. Some of these will be above the content in the course, but that’s also a right-of-passage for coders: looking up code online and trying to decipher how it works to solve a problem you have. Lean into the unpuzzling and deciphering of using online resources to find solutions!

Hope this helps,  
Dan

---

<div class="post-metadata">

**Author:** ![bnarsiman](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bnarsiman/32/6759_2.png) [@bnarsiman](https://forum.code.org/u/bnarsiman)\
**Post date:** [February 1, 2024, 11:47pm UTC](https://forum.code.org/t/coding-and-debugging-with-circuit-playground/38955/3 "2024-02-01T23:47:17Z")

</div>

Thank you so much Dan, I’ll pass on your suggestions to the student.  
Much appreciated.  
Take care  
Bron
