Coding/Blog challenge 10

Coding/Blog challenge 10

10th installment

About a 1/3rd of the way through this challenge now. I've been enjoying it! I will say though that after taking a few weeks away from a project I was working on, diving back into the codebase is a bit weird.

Problems: Everyone's got 'em

But this is my current problem. I got this one from Edabit, & I cannot remember the difficulty level, either hard or medium. I was going through both categories, solving random problems for fun.

JsProblem10.png

Peer into my brain

For this challenge, I am thinking that I will need to iterate over an integer. In JavaScript, you cannot iterate over an integer data type. So, I'll need to convert it to a string or an array. Let's set that up now, I chose to just do it as a string.

//here we initialize our function
function oddishOrEvenish(num) {
    // now we convert to  string data type
    num = num.toString();
    return;
}

After this, I want to declare a new variable of int type. This is what I will use to add all the numbers together. We want to find the sum of all the numbers that make up our integer argument. If our argument was 1234 we want to add 1+2+3+4 = 10. In order to do that, we need to initialize a loop to iterate over the arguments numbers and use the addition assignment operator.

function oddishOrEvenish(num) {
    num = num.toString();
    //the declaration a new integer which starts at zero
    let newNum = 0;
    //now iterate over number
    for (let i = 0; i < num.length; i++) {
        //use the  addition assignment to add each number consecutivly 
        newNum += Number(num[i]);
    }

   return;
}

Finally, we want to make our output. We will use the remainder operand to figure out if the sum of all the numbers is even or odd. If the remainder of the sum when divided by 2 is equal to 0 then we will know that we have an "evenish" number, if the remainder isn't 0 then we will know that it is "oddish." I will use an if statement to do this.

function oddishOrEvenish(num) {
    num = num.toString();
    let newNum = 0;

    for (let i = 0; i < num.length; i++) {
        newNum += Number(num[i])
    }

    if (newNum % 2 == 0) {
        return "Evenish"
    }
    return "Oddish"
}

And now, all the tests pass, & we can move on to the next challenge. Thanks for following my journey!