2

Write a function named "find_value" that takes a key-value store as a parameter with strings as keys and integers as values. The function returns a boolean representing true if the value 7 is in the input as a value, false otherwise. (My code below)

function find_value(key){
    for (var i of Object.values(key)){
        if (i == 7){
            return true;
        }
        else{
            return false;
        }
    }
}

When I call with the input [{'effort': 4, 'doctrine': 8, 'item': 11, 'behavioral': 7, 'occasional': 11}], I should get true, but I am getting false instead. What am I doing wrong?

3
  • 1
    Move return false outside the loop. Commented Oct 10, 2018 at 20:00
  • 1
    return breaks the loop Commented Oct 10, 2018 at 20:02
  • What happens if the key value is 4, or 8, or 11? I mean, read each line and tell yourself what happens if i is equal to anything but 7 while inside that loop? If you print the value of i to the console inside the loop, what results do you get in the console? ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Oct 10, 2018 at 20:04

5 Answers 5

1

Your loops is returning false on your first item, most likely. You should keep the condition and return true statement in the loop, but after the loop you should return false.

P.S. You should name your parameter and set a default value for good practice. I renamed it in my example below.

function find_value(myObject = {}){
    for (var i of Object.values(myObject)){
        if (i == 7){
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have 2 problems here:

  1. You are passing an array with an object inside.
  2. In else you are returning false, so function will test only the first value.

BTW it could be done this way:

return Object.values(obj).includes(7);

Comments

0

In order for this to work as expected, you should pass an object into find_values(), rather than an array as you are doing. Also, you will want to update the flow of your function, by returning false after loop iteration has completed:

function find_value(key){
    for (var i of Object.values(key)){
        // Only early return if matching value found
        if (i == 7){
            return true;
        }
    }

    // Move return false outside of loop
    return false;

}

var object_input  = {'effort': 4, 'doctrine': 8, 'item': 11, 'behavioral': 7, 'occasional': 11};

console.log( find_value(object_input) );  

Comments

0

Try

function find_value(key){
    for (var i of Object.values(key)){
        if (i == 7){
            return true;
        }
    }
}

1 Comment

A note with this, is that it returns undefined if the value is not matched which strictly speaking, is not equivalent to returning "false"
0

Your function automatically returns true or false in the first iteration. The simplest solution that is very beginner-friendly is to set a boolean flag before the loop and check it at the end of the loop

Also you were looping through the array and checking for a value when the loop would return you an object with property + value pairs. You want to get the values of those objects and check if it is equal to 7

function find_value(key){
    let flag = false;
    for (let i of key){
        console.log(Object.values(i));
        if (Object.values(i).indexOf(7) > -1) {
            flag = true;
        }
    }
    return flag;
}

console.log(find_value([{'effort': 4, 'doctrine': 8, 'item': 11, 'behavioral': 7, 'occasional': 11}]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.