# 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] != int) {
let newarr = [int,1]
single.push(newarr);
i = 1;
a++;
}
else {
i++
single[a] = i;
}
}
let odd;
for (let ele of single) {
if (ele % 2 == 1) {
odd = ele;
}
}
return odd;
}
``````

Hope this helps

``````function findOdd(arr) {
return +Object.keys(arr.reduce((integers, num) => {
if (integers[num]) {
delete integers[num];
} else {
integers[num] = 1;
}
return integers;
}, {}));
}
``````

The reduce method used here ends up creating an object with only one property. It does it by either adding the number as a property name if the number does not already exist as a property in the object or deleting the property if it does exist. The Object.keys part is to extra the single property left in the object returned from the reduce method.

Below uses the same logic but instead of an object, a Map is used.

``````function findOdd(arr) {
return arr.reduce((integers, num) => {
if (integers.has(num)) {
integers.delete(num);
} else {
integers.set(num, 1);
}
return integers;
}, new Map()).keys().next().value;
}
``````

The main thing to understand about this approach is since you know there is only one integer that occurs an odd number of times, you can simply delete it (the property name) from the numbers seen so far. Why? Because if the number appears twice (and that is it), then you know it does not appear an odd number of times, so you would not include it as a possible candidate for actual final integer to return.

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?

There is, but it is very messy and not worth it when there are other ways (like using the filter or find methods) to accomplish what you want.

1 Like