I need A hand here!

/* write a function to check if each element of an array has been broken down to odd number
And return an object which contain each element of the array as the key
and the number of division element went through before becoming odd number as the value */


function toOddNumber(arr){
    let count = 0;
    let myObj ={};

    arr.filter(function recus(num){
        if(num == 1){
             myObj[num] = 0;
            
        }
        let x = parseInt(num/2);
        count ++;
        if(x % 2 == 1){
            return myObj[num] = count;   
        }
        
    });

    return myObj;

}
const arry = [1, 3, 4, 5, 7, 8];
console.log(toOddNumber(arry));

Iā€™ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The ā€œpreformatted textā€ tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

I donā€™t really get what you need to do, but you canā€™t use filter like that, filter wants a callback that returns true or false and will return an array with only the elements for which the callback returned true
if this feature of filter doesnā€™t interest you, you should use forEach

thanks boss!, I will watch out next time

thanks, try out forEach, but I learnt that it can mutate the array that is the reason for running away from itā€¦
Anyway, the mind set behind this code is to iterate through the array and divide each element until such element get to odd number, then it will be add to an empty object created as key/property either, then the value will be the number of times it gone through the process of divisionā€¦
Example:
let say the first element of the array is 4, it will under go division twice before it become an odd number, then it will be returned as {4: 2}

it can mutate the array, doesnā€™t mean that it willā€¦ it depends on the callback you are using. but the way you are using filter is simply terrible. for the callback you are using, and for the fact that you are not interested in the returned array, forEach is better. or just use a loop.


note that for where count is, it will keep increasing for each element of the array.

thanksā€¦
but kindly help with the code or give me the Algorithm that will open my eye to something am missing here.
though am thinking of adding a recursive but each time I do, I error of stack overflow stuff like thatā€¦
kindly help me out if you understand the logic behind the code

I am trying to help you

count is being set to 0 in the function, then you do the logic for each array element to check how many times you need to divide the number, and count keep increasing, keep increasing, keep increasingā€¦
if you initialise count there, you never get the value you need

in this challenge there is a similar issue:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/use-caution-when-reinitializing-variables-inside-a-loop

if you donā€™t see the issue try looking at your code with this tool:
http://pythontutor.com/javascript.html#mode=edit

an other thing is that you divide each number only once. if you have 64, it will never become odd, as you divide only once

ā€œan other thing is that you divide each number only once. if you have 64, it will never become odd, as you divide only onceā€

am thinking of recursive call but I dont know how to go about it

Before jumping into a recursive solution, I would highly suggest doing it with a regular for loop so you can understand whatā€™s going on, which will make it 100x easier when trying to convert it to a recursive solution.

instead of recursion, try with loops
you can check a while loop or for loop

I would also suggest not using a method to iterate over the array, but trying everything with loops

thing is given headache, now at this, have tried to break it down to simple problem like using one element a time not even array again, but am having problem with the place to put the count variable which will make the whole fun
take a look

function recus(num){
    let count = 0;
    let myObj ={};  
    if(num == 1){
         myObj[num] = 0;    
    }
    else{
        count++
        let x = parseInt(num/2);
        
        if(x % 2 !== 0){
            myObj[num] = count;   
        }
        else{
            recus(x);
            
            myObj[num] = count; 

        }
    } 
    return myObj;   
}
console.log(recus(8));

using this: http://pythontutor.com/javascript.html#mode=edit
I can see that the recursive call is working but not rendering its return, instead it will pick up from where it stop before been called and the return will be a flop!
kindly explain somethingā€¦

whatā€™s the code you are looking on python tutor? the last one you posted?

you have recus(x) in your code, but you are doing absolutely nothing with the returned value

imo, recursion is not at all a good way to solve this. loops would be much better

" you have recus(x) in your code, but you are doing absolutely nothing with the returned value"
That is my pointā€¦
I how can I do something with the return value, I only need count value that recursionā€¦ How can I get it.
Then I donā€™t know how I can use only loop to solve this problem, because I will be dividing repeatedly until I have a odd number from the input value.
For instance, if input is 8, I have to divide it until I have a odd number, so the return value will now be an object consisting of the 8(which is input) as key and the number of division it undergo as(count) as the value.
This is not a challenge from anywhere, is what I just think of by myself and I believe if I could get through it, it can be applied to solve real life problem in the near future.
So put me through, use assumption to point me to relevant places where am making mistakeā€¦rather than merry-go-roundā€¦you can sight a closely related example, things like thatā€¦
We have talked to much on this problem.
I have display all I could, I even try to break it down in to simpler problem using a single input not an array again, believing if I could get it working with that, then I can come up with the array stuff all from theirā€¦at least thatā€™s the way I have been reading it, ā€˜break big or huge problem into simpler one and tackle it from there.ā€™

for example, I have a number, I want to subtract 1 till I have 0

let x = 56;
while (x > 0) {
   x--;
}

you can do something really similar for division till the number is odd

can you show that in pseudocode? break it down to the smallest steps with pseudocode
because then who help you can see if the issue is with the logic you are using to solve the algorithm or with misunderstanding of how things work

1 Like

Thank you, am grateful
the insight you provided give me the edge wayā€¦thanks
have crushed itā€¦

    let myObj ={};  
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == 1){
            myObj[arr[i]] = 0; 
               
       }
       else{
           let count = 0;
           let x = parseInt(arr[i]/2);
           count ++;
           while(x % 2 == 0){
               x /=2
                count++;
           }
           myObj[arr[i]] = count;
       }

    } 
    return myObj;   
}

const arry = [1, 3, 4, 5, 7, 8];
console.log(recus(arry));

Can you tell explicitly what I should do to the recursive call I declareā€¦In case of next timeā€¦ I want to know how to use recursionā€¦ But all the articles have seen even videos have not given me a clear explanation, or let me say they have not explain in layman terms that a baby in coding like me can digest.
I will be grateful if you can do somethingā€¦

in this case I wouldnā€™t know how to use recursion easily

but, you need to think of a stopping condition

this is calculating a factorial using recursion, notice: specific conditions to stop the recursion, and then the recursive call, where the inputted value change at each call of the function
try looking at this with the python tutor tool

function factorialize(num) {
   if (num < 0) {
      return null;
   }
   if (num === 0) }
      return 1;
   }
   return num * factorialize(num-1);
}

factorialize(4);