# Comparing Variables App Lab

**URL:** <https://forum.code.org/t/comparing-variables-app-lab/37829>\
**Category:** Coding and Debugging Help\
**Tags:** csd-unit-4\
**Created:** [March 30, 2023, 2:00pm UTC](https://forum.code.org/t/comparing-variables-app-lab/37829 "2023-03-30T14:00:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mriley](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mriley](https://forum.code.org/u/mriley)\
**Post date:** [March 30, 2023, 2:00pm UTC](https://forum.code.org/t/comparing-variables-app-lab/37829/1 "2023-03-30T14:00:12Z")

</div>

Students are trying to compare the variables values to switch to a recommend screen.  
We can’t figure out what the issue is.  
Any thoughts would be helpful and appreciated  
The first selection work, but the others do not, we have tried \> and the && and the || without success.

[[App Lab - Code.org](https://studio.code.org/projects/applab/yyriowXePDeFZt82SmkSdRhqLzlfnf8A24wWCcPNMPM)]([https://Link](https://Link) to project)

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [March 30, 2023, 6:30pm UTC](https://forum.code.org/t/comparing-variables-app-lab/37829/2 "2023-03-30T18:30:30Z")

</div>

May you describe the problem more specifically?

---

<div class="post-metadata">

**Author:** ![mriley](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mriley](https://forum.code.org/u/mriley)\
**Post date:** [March 30, 2023, 6:43pm UTC](https://forum.code.org/t/comparing-variables-app-lab/37829/3 "2023-03-30T18:43:10Z")

</div>

The user of the project should be able to select the buttons on the various screens. The buttons add value to the variables.  
At the final screen, the user should select “recommend” and the project should recommend a “player” based on the variable that has the greatest value.  
When we run the project, we are not directed to the screen with the greatest variable value, we are only directed to “Alex”  
cant figure it out

---

<div class="post-metadata">

**Author:** ![ismailmf777](https://avatars.discourse-cdn.com/v4/letter/i/d9b06d/32.png) [@ismailmf777](https://forum.code.org/u/ismailmf777)\
**Post date:** [April 8, 2023, 9:53am UTC](https://forum.code.org/t/comparing-variables-app-lab/37829/4 "2023-04-08T09:53:05Z")

</div>

Instead of checking the value of each variable seperately as was attempted in the code, you should include them in an array and iterate through the array elements. I changed your `recFunction`. It works perfectly now.

```auto
function recFunction() {
  var highest;
  var highestScore=0;
  var choices=[widowmaker,ghost,pyramidhead,chunli,bayonetta,cloud,jetstreamsam,alex];
  var names=["Widowmaker","Ghost","PyramidHead","ChunLi","Bayonetta","Cloud","JetSam","Alex"];
  for (var i=0; i<choices.length; i++){
    if (choices[i]>highestScore){
      highestScore=choices[i];
      highest=names[i];
    }
  }
  console.log(highest);
  setScreen("Choice_"+highest);
}

```

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [April 8, 2023, 1:59pm UTC](https://forum.code.org/t/comparing-variables-app-lab/37829/5 "2023-04-08T13:59:28Z")

</div>

Hi @mriley,

@ismailmf777 has a good solution but I’m guessing your students might not be familiar with arrays. Boolean expressions need 2 arguments to evaluate to true or false - this is why your code may not be working. To simplify the problem within the skill-set your students are familiar with, you can set the first variable to the max value and then compare that to the other values. An example might be:

//Initialize the maximum value variable  
max\_val = alex

//compare each value to the max value.  
if cloud \> max\_val:  
max\_val = cloud  
if chunli \> max\_val:  
max\_val = chunli  
if bayonetta \> max\_val:  
max\_val = bayonetta  
if ghost \> max\_val:  
max\_val = ghost  
if jetstreamsam \> max\_val:  
max\_val = jetstreamsam  
if pyramidhead \> max\_val:  
max\_val = pyramidhead  
if widowmaker \> max\_val:  
max\_val = widowmaker

Within each if statement, you can show the sprite for the max\_val. By the end of the series of if statements, the correct sprite should be showing.

Good luck!  
~Michelle

---

<div class="post-metadata">

**Author:** ![mriley](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mriley](https://forum.code.org/u/mriley)\
**Post date:** [April 14, 2023, 5:31pm UTC](https://forum.code.org/t/comparing-variables-app-lab/37829/6 "2023-04-14T17:31:15Z")

</div>

Thank you both for these responses! Both are VERY helpful and very appreciated!
