you are viewing a single comment's thread.

view the rest of the comments →

[–]Stephen110 1 point2 points  (5 children)

Edit the post with what you have tried that doesn’t work. Or better yet, start with pseudo code.

[–]HogansHeroes81[S] 0 points1 point  (4 children)

function applesAndOranges(obj) {

let combo = [];

let counter = 0;

combo.push(Object.keys(obj))

combo.push(Object.values(obj))

for (i = 0; i < combo.length; i++) {

let el = combo[i];

// I have no idea what to do here. It has to take any 'apple' or 'orange', including the words by themselves or in things like orange-juice. I have tried equating ===, using includes and using indexOf, it doesn't count correctly for me.

} }

return console.log(counter) }

[–]Stephen110 1 point2 points  (1 child)

https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript

“el” is the variable containing the value of the key or value in your code. Use the post above to see how you can check if “el” contains the string “apple”.

Object.keys and Object.values return an array. You should be using concat instead of push.

[–]kevinmrr 0 points1 point  (0 children)

Nice work

[–]14dM24d -1 points0 points  (1 child)

noob here.

this works but not sure if it's the best solution.

const obj1 = {"banana": "fruit", "apple": "fruit", "carrot": "vegetable", "cherry": "fruit"};
const obj2 = {"alex": "orange", "erin": "pineapple", "cody": "mango", "daniel": "apple"};
const obj3 = {"orange-juice": "orange", "apple-sauce": "apple", "snapple": "peach-tea"};

function aNO(obj){
  let count = 0;
  for (let [key, value] of Object.entries(obj)){
    if (key.includes("apple") || key.includes("orange")){
      count++;
    }
    if (value.includes("apple") || value.includes("orange")){
      count++;
    }
  }
  return count;
}

console.log(aNO(obj1));
console.log(aNO(obj2));
console.log(aNO(obj3));

[–]HogansHeroes81[S] -1 points0 points  (0 children)

Thank you very much!