If function for remainder operator (not equal to 0)

Hi i seem to be struggling with this. i want to find the odd number

if ((storedNumbers[storedNumber] % 2) != 0) {
      return storedNumber;

it returns undefined. How can i loop through each object value and return the key once i find an odd number. I thought != would work,

Any idea? thanks

function findOdd(A) {
  //create an empty object that stores count of numbers
  storedNumbers = {};

  //iterate through the array and add 1 to the counter (and create the object if it has not already been counted)
  for (i = 0; i < A.length; i++) {
    currentValue = A[i];
    if (storedNumbers[currentValue] >= 1) {
      storedNumbers[currentValue] = storedNumbers[currentValue] + 1;
      console.log(storedNumbers);
    } else {
      storedNumbers[currentValue] = 1;
      console.log(storedNumbers);
    }
  }
  //   //for each object, run a loop that %2 = 0. then return that number
  //   const valuesList = Object.values(storedNumbers);
  //   console.log(valuesList);

  //   for (i = 0; i < valuesList.length; i++) {
  //     currentValue = valuesList[i];
  //     if (valuesList[currentValue] % 2 == 0) {
  //       console.log("no problem");
  //     }
  //     if (valuesList[currentValue] % 2 != 0) {
  //       return valuesList[currentValue];
  //       console.log("found");

  const run = Object.keys(storedNumbers).forEach(storedNumber => {
    console.log(storedNumber); // the name of the current key.
    console.log(storedNumbers[storedNumber]); // the value of the current key.
    if ((storedNumbers[storedNumber] % 2) != 0) {
      return storedNumber;
    }
  });
}

findOdd([20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5]);

Hey,

function findOdd(array){
  let result = [ ] ;
  for (let i = 0 ; i < array.length ; i++){
    if ( array[i] % 2 === 1){
    result.push(array[i] );
   }
}
return result;
}

i keep getting undefined. if i define result outside the function i get an error that result is not defined ??

 function findOdd(storedNumbers) {
    let result = [];
    for (let i = 0; i < storedNumbers.length; i++) {
      if (storedNumbers[i] % 2 === 1) {
        result.push(storedNumbers[i]);
      }
    }
    return result;
}

findOdd([20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5]);

Hello silvercut.

Have you commented out all of the code you have above the findOdd() function? The code Jlassi gave is all the code you need to return an array of odd numbers.

function findOdd(A) {
  //create an empty object that stores count of numbers
  storedNumbers = {};

  //iterate through the array and add 1 to the counter (and create the object if it has not already been counted)
  for (i = 0; i < A.length; i++) {
    currentValue = A[i];
    if (storedNumbers[currentValue] >= 1) {
      storedNumbers[currentValue] = storedNumbers[currentValue] + 1;
      console.log(storedNumbers);
    } else {
      storedNumbers[currentValue] = 1;
      console.log(storedNumbers);
    }
  }

  // Object.keys(storedNumbers).forEach(storedNumber => {
  //   console.log(storedNumber); // the name of the current key.
  //   console.log(storedNumbers[storedNumber]); // the value of the current key.
  //   let currentVal = storedNumbers[storedNumber];
  //   if (currentVal % 2 !== 0) {
  //     return storedNumber;
  //   }
  // });
  // return storedNumber;

  function findOdd(array) {
    let result = [];
    for (let i = 0; i < array.length; i++) {
      if (array[i] % 2 === 1) {
        result.push(array[i]);
      }
    }
    return result;
  }

  findOdd(storedNumbers);
}

findOdd([20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5]);

its required i return the key from the key value pair of the odd number

Could you give an example of what you want your object to look like? What is the output you want, exactly as it should be displayed? Because I am unsure why you are using an object.

this is the challenge. i want to return the key from the key value pair.

Ah…That makes more sense. The challenge is asking for an integer from an array that is repeated an odd number of times.

Well, this is how I did it using arrays, not objects:

function findOdd(A) {
  const arr = A.sort();
  let single = [[0,0]];
  a = 0;
  let i = 1;
  for (let int of arr) {
    if (single[a][0] != int) {
      let newarr = [int,1]
      single.push(newarr);
      i = 1;
      a++;
    }
    else {
      i++
      single[a][1] = i;      
    }
  }  
  let odd;
  for (let ele of single) {
    if (ele[1] % 2 == 1) {
      odd = ele[0];
    }
  }
  return odd;
}

Hope this helps

Hi thanks, actually i am confused specifically as to why the ForEach solution doesn’t work? What makes it return undefined?

Works (For in loop)

function findOdd(A) {
  //create an empty object that stores count of numbers
  storedNumbers = {};

  //iterate through the array and add 1 to the counter (and create the object if it has not already been counted)
  for (i = 0; i < A.length; i++) {
    currentValue = A[i];
    if (storedNumbers[currentValue] >= 1) {
      storedNumbers[currentValue] = storedNumbers[currentValue] + 1;
      console.log(storedNumbers);
    } else {
      storedNumbers[currentValue] = 1;
      console.log(storedNumbers);
    }
  }

  for (const number in storedNumbers) {
    const count = storedNumbers[number];
    // if the count is odd
    if (count % 2 !== 0) {
      // return that number
      return +number;
    }
  }
}

  findOdd([20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5]);

doesn’t work (for each loop):

function findOdd(A) {
  //create an empty object that stores count of numbers
  storedNumbers = {};

  //iterate through the array and add 1 to the counter (and create the object if it has not already been counted)
  for (i = 0; i < A.length; i++) {
    currentValue = A[i];
    if (storedNumbers[currentValue] >= 1) {
      storedNumbers[currentValue] = storedNumbers[currentValue] + 1;
      console.log(storedNumbers);
    } else {
      storedNumbers[currentValue] = 1;
      console.log(storedNumbers);
    }
  }

Object.keys(storedNumbers).forEach(storedNumber => {
    console.log(storedNumber); // the name of the current key.
    console.log(storedNumbers[storedNumber]); // the value of the current key.
    let currentVal = storedNumbers[storedNumber];
    if (storedNumbers[storedNumber] % 2 !== 0) {
      return +storedNumber;
    }
  });
  
}
    findOdd([20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5]);

its odd since the console.logs log fine…

Because a return statement in the forEach method does not return back to the calling function. You use the forEach method to iterate through an array without stopping to return something.

const arr = [ 1, 2, 3, 4, 5, 6, 7 ];
arr.forEach(elem => {
  console.log(elem)
  return "This does not return anything.";
});

The above just displays:

1
2
3
4
5
6
7
1 Like

amazing thanks for the quick response. is there no way to alter the forEach to allow for return statements at all?