# Why is this code not return true?

**URL:** <https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799>\
**Category:** General\
**Created:** [May 12, 2018, 3:14pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799 "2018-05-12T15:14:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bego96](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bego96/32/73440_2.png) [@Bego96](https://forum.freecodecamp.org/u/Bego96)\
**Post date:** [May 12, 2018, 3:14pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/1 "2018-05-12T15:14:35Z")

</div>

Ok so im trying to return album’s name of Judas Priest but it keeps return false aka Nothing. Any tips?

```auto

var myStuff = [
    {
        "name" : "Selena Gomez",
        "album" : "Stars Dance",
        "year" : 2013
    },
    
    {
        "name" : "Judas Priest",
        "album" : "Rocka Rolla",
        "year" : 1974
    }
];

function myFunction(name) {
 
for (var i = 0; i < myStuff.length; i++) {
    if (myStuff[i].album === name) {
        return myStuff[i];
    } else {
        return "Nothing";
    }
}
}
myFunction("Rocka Rolla");

```

---

<div class="post-metadata">

**Author:** ![MarioLema](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mariolema/32/83368_2.png) [@MarioLema](https://forum.freecodecamp.org/u/MarioLema)\
**Post date:** [May 12, 2018, 3:29pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/2 "2018-05-12T15:29:14Z")

</div>

Although your return is inside a for loop and it would appear to work for each item on the array, the fact is that it is still in the same scope as the function, so if `myStuff[i].album === name` is run when i equals 0 and they don’t match the return will affect the whole function and leave without doing any more operations.

For loops do not have returns so you can’t return to the top of the loop or give a different return for each item you are iterating through.

In this case the easier way is to assign the value of myStuff[i] to a variable if it matches with name and return it outside of the for loop.  
If you want to return ‘Nothing’ you can either declare the initial variable with the value of ‘nothing’ and it will be changed if there is a match or declared and undefined variable and do a final if statement where if that variable is undefined you return the string ‘Nothing’

---

<div class="post-metadata">

**Author:** ![Bego96](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bego96/32/73440_2.png) [@Bego96](https://forum.freecodecamp.org/u/Bego96)\
**Post date:** [May 12, 2018, 3:33pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/3 "2018-05-12T15:33:18Z")

</div>

Can you make a code example of what you mentioned about variable assignment? I tried doing this simmilar to challenge lookup profile [freeCodeCamp Challenge Guide: Profile Lookup](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259) So its strange how it doesnt work on mine.

---

<div class="post-metadata">

**Author:** ![MarioLema](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mariolema/32/83368_2.png) [@MarioLema](https://forum.freecodecamp.org/u/MarioLema)\
**Post date:** [May 12, 2018, 3:39pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/4 "2018-05-12T15:39:58Z")

</div>

```auto
var numbers = [6,5,3,8,2]

function myFunction(num) {
 var answer = 0;
for (var i = 0; i < numbers.length; i++) {
    if (numbers[i] === num) {
        answer = num;
    } 
}
return answer;
}
myFunction(3);

```

Here, the function will return the value of answer, if the number passed is in the array, answer will equate to that number otherwise it will remain as 0.

---

<div class="post-metadata">

**Author:** ![Bego96](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bego96/32/73440_2.png) [@Bego96](https://forum.freecodecamp.org/u/Bego96)\
**Post date:** [May 12, 2018, 3:57pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/5 "2018-05-12T15:57:46Z")

</div>

Ok thank you, do you know any sources where i can practice for loop’s, if’s, and n esting if’s with loops, switch etc…?

---

<div class="post-metadata">

**Author:** ![MarioLema](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mariolema/32/83368_2.png) [@MarioLema](https://forum.freecodecamp.org/u/MarioLema)\
**Post date:** [May 12, 2018, 4:02pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/6 "2018-05-12T16:02:35Z")

</div>

Codewars is a great page to practice functions, you can search for loops too. 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 1:06pm UTC](https://forum.freecodecamp.org/t/why-is-this-code-not-return-true/190799/7 "2021-01-16T13:06:57Z")

</div>

Closed on January 16, 2021, 1:06pm UTC


