Print Out Array

How to print only odd numbers from this array using funtion
" var num = [1,2,3,4,5,6,7,8,9,10];

Hello!

What have you tried so far? You can find if a number is odd or not by checking is remainder, and you can do that with the % sign. Search about that, and see if you can solve it by yourself.

Good luck!

i did it by for loop but i dnt know how to do it in array.

var arr = [1,2,3,4,5,6,7,8,9,10]
function arrSum(array) {
var sum = 0;
for (var i = 0; i < array.length; i++) {
sum = sum + array[i];
}
console.log(sum);
}
arrSum(arr);

i calculate the sum of the array like but having problem now to print only odd number of this array

Did you research the remainder operator like it was suggested?

Check if Number is Even/Odd

i willsearch for it thank you

Hi @farooq83

Hope your doing well, i know your frustration when it comes on learning new skill and more for a complex language of Javascript and fun at the same time. I know your looking for help and sometimes you want a bit more than just words, because i was there too, but i wanted to share an example of what you want to archive or have doughts regarding how to do things, on the code i explain every detail, on how to get your issue resolved, but at the same time i want you to learn and not just copy paste, to really work on every line of code and understand it, that way you will be familiar with it, if you dont get something search for it, like you said or ask the forum there are well know and good experts out here. Hope this is not to late message, but im sure will not be late for you to go back and learn something good at it. Happy Coding =)

// We create our numbers array from the outside scope.
var numbers = [1,2,3,4,5,6,7,8,9,10];
// We create an empty array of odd Numbers in case, you want to store them in an array as well.
var oddsNum = [];

// we create an annonimus function and pass a parameter of the 'numbers array []'
var evenOddsNumbers = function(numbersArray) {
    // We loop for all the numbersArray.
    for (var i = 0; i < numbersArray.length; i++) {
        /*
            1. We validate if the arrays are even/odd by using the Reminder Operator,
            2. the result of the Reminder will be equal or not equal to 1, that will decide were to go for even/odd numbers on the array.
            3. I added two options even/odd results.
        */
        if ((numbersArray[i] % 2) != 1) {
            var evens = numbersArray[i];
            //console.log(evens)
        }else {
            // we create an varriable to store all odd numbers and display them in the console.
            var odds = numbersArray[i];
            // Display the result in a console, for all odd numbers.
            console.log(odds);

            // Optional: We Display the odd numbers into an empty arrray.
            //oddsNum.push(numbersArray[i]);
            //console.log(oddsNum);
        }
    }

};

// We call the funtion, and pass an argument of numbers arrays.
evenOddsNumbers(numbers);
1 Like

@imendieta By posting the actual code, you have made it so the user can copy/paste it. It is always best to give hints and examples instead of just giving the solution away. Nothing is learned otherwise. Most people can “understand” once they see a solution, but until they create their own solutions, showing them does not help much. Now after someone has already created a successful solution, it is great to see other ways of doing the same thing.

1 Like