Javascript Even and Odd Range

Javascript array using forloop , odd and even number goes out of the range i had set

Your mistake is checking the value of i, not the value of intArray[i], and pushing the value of i, not the value of intArray[i] into the even/odd arrays.
fixed your code, now it should work as expected.

var intArray = [],
evenArray = [],
oddArray = [],
i;

for (i = 0; i <= 50; i++) {
intArray[i] = Math.floor(Math.random() * 10) + 1;
}

for (i = 0; i < intArray.length; i++) {
if (intArray[i] % 2 == 0) {
evenArray.push(intArray[i]);
} else {
oddArray.push(intArray[i]);
}
}

console.log('Original Array: \n' + intArray.join(' '));

console.log('Odd Array: \n' + oddArray.join(' '));

console.log('Even Array: \n' + evenArray.join(' '));

Building a JavaScript grid with odd and even characters using two loops

Try this
if ((i+j) % 2)

function gridGenerator(num) {  var grid = '';  var row = '';
for (var i = 0; i < num; i++) { for (var j = 0; j < num; j++) { if ((i+j) % 2) { row += '_'; } else { row += '#'; } } grid += row.slice(-num) + '\n'; } return grid;}
console.log(gridGenerator(4));

Javascript Odd or even Algorithm question?

= is an assignment operator. It assigns the value of the expression on the right-hand side to a variable on the left-hand side.

== is an equality operator. It tests the equality of the result of both the left and right-hand-side expression.

Since you're using = and inadvertently assigning number to the value of your expression on the right, it will always evaluate to a truthy value and return "Even". To fix, remove the number = from each of the expressions:

function even_or_odd(number) {  if(number % 2 == 0) {  return "Even"  }else if(number % 2 !== 0) {  return "Odd"  }};
console.log(even_or_odd(0));console.log(even_or_odd(1));

Counting odd and even numbers function issue

Your answer is not working because a number is not iterable, you should cast it to string first.

function parseNum(num) {
var obj = {
odd: 0,
even: 0
};
if (typeof num === 'number') {
num = num.toString();
}
var arr = Array.from(num);
arr.forEach(function(value) {
if (value % 2 === 0) {
obj.odd += 1;
} else {
obj.even += 1;
}
});

return obj;
}

Javascript code to separate even and odd numbers then get their respective sums and averages

You are doing following things wrong

  • You are adding all the previous values of arrayOdd and arrayEven again and again. You should add the new value to the sumOdd and sumEven.
  • You are calculating average before all inputs are completed. You should calculate avgO and avgE at end of loop.

var arrayNum = [];var arrayEven = []; var arrayOdd = []; var i; var NUM_INPUTS;  var sumOdd = 0.0;var sumEven = 0.0; var avgO = 0.0;var avgE = 0.0;
NUM_INPUTS = parseInt(prompt("Enter the number of inputs you need: "));for (i = 0; i < NUM_INPUTS; i++) { arrayNum.push(parseFloat(prompt("Enter the numbers: " + (i + 1)))); if ((arrayNum[i] % 2) === 1) { arrayOdd.push(arrayNum[i]); sumOdd += arrayNum[i]; } else { arrayEven.push(arrayNum[i]); sumEven += arrayNum[i]; }}avgO = sumOdd / arrayOdd.length;avgE = sumEven / arrayEven.length;//Output resultsdocument.write("All numbers in the array are: " + arrayNum);
document.write("<br/>All even numbers in the array are: " + arrayEven);document.write("<br/> The sum of all even numbers is: " + sumEven + " and average of the even numbers is: " + avgE);
document.write("<br/>");document.write("<br/>All odd numbers in the array are: " + arrayOdd);document.write("<br/> The sum of all odd numbers is: " + sumOdd + " and the average of the odd numbers is " + avgO);


Related Topics



Leave a reply



Submit