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
andarrayEven
again and again. You should add the new value to thesumOdd
andsumEven
. - You are calculating average before all inputs are completed. You should calculate
avgO
andavgE
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
How to Wait Until All JavaScript Files Are Loaded Before Executing JavaScript Code
How to Replace All Words of Length Less Than 3 in a String With " " (Space) in JavaScript
How to Validate Mobile Number for Countries in Angular
How to Monitor Changes to an Object
Increase JavaScript Heap Size in Create-React-App Project
How to Get Parent Width/Height in React Using Hooks
How to Prevent a Key Input from Appearing in Input Field
How to Determine Server Disconnection from Signalr Client
Disable Pdf Download and Save Option
Adding Whitespace to a String in Typescript (In Angular)
How to Detect If Two Divs Are Touching/ Collision Detection
Detect If Html5 Video Element Is Playing
Getting an Array of Column Names At Sheetjs
How to Get Convert an Object to HTML Markup
Displaying Navbar on Multiple HTML Pages Using Bootstrap
How to Refresh the Data in a Jqgrid