This question already has answers here:
Why does console.log return undefined after correct output?
(1 answer)
Javascript even and odd range
(6 answers)
How to append something to an array?
(30 answers)
Closed 9 months ago.
I am creating a function which takes in a minimum and max value. The output should list all the even numbers between these two parameters. I have attempted at creating an array to display these numbers but I get this output:
evenNumbers(4,13) returns: undefined
evenNumbers(3,10) returns: undefined
evenNumbers(8,21) returns: undefined
How to fix my code so it shows the list properly?
var evenNumbers = function(minNumber, maxNumber){
var list = [];
for (let i = minNumber; i < maxNumber; i++){
if (i % 2 == 0){
for(let k = 0; k < maxNumber; k++){
i = list[k];
}
}
}
return console.log(list);
}
console.log('evenNumbers(4,13) returns: ' + evenNumbers(4,13));
console.log('evenNumbers(3,10) returns: ' + evenNumbers(3,10));
console.log('evenNumbers(8,21) returns: ' + evenNumbers(8,21));
This question already has answers here:
How to determine if a number is odd in JavaScript
(31 answers)
Closed 1 year ago.
I'm trying to merge this two codes to run as a working function that print out odd and even numbers.
but I don't know on how to use a var
let num = [1,2,3,4,5,6,7,8,9,];
console.log('even numbers are');
for (var i = 1 ; i < 10 ; i += 2 ) {
console.log(i);
}
console.log('odd numbers are ');
for (var i = 2 ; i < 10 ; i += 2 ) {
console.log(i);
}
Help. Thanks
const isOdd = (n) => (n & 1) === 1;
const num = [1,2,3,4,5,6,7,8,9];
console.log( `Odd numbers are ${ num.filter( n => isOdd(n))}` );
console.log( `Even numbers are ${ num.filter( n => !isOdd(n))}`);
const
Arrow functions
Bitwise AND (&)
Logical NOT (!)
Array.prototype.filter()
Use below code to print out Even Numbers and in the same way you can get Odd numbers list given in an Array
var arr = [1, 2, 3, 4, 5, 6];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
console.log(arr[i] + "");
}
}
If you are trying to put your JS code inside HTML Page, refer to the link for more details Find Even Numbers in JS
This question already has answers here:
How to create an array containing 1...N
(77 answers)
Fastest way to fill an array with multiple value in JS. Can I pass a some pattern or function to method fill insted of a single value in JS? [duplicate]
(1 answer)
Closed 2 years ago.
Been having an issue with this and don't know where to start. I am programming this with JavaScript. How do I create an array containing the multiples of 4 between 20 and 800, inclusive?
Try this
let arr = [];
for(let i=20; i <= 800; i++) {
if(i%4 === 0) {
arr.push(i)
}
}
console.log(arr);
It will have all element which is multiple of 4 between 20 and 800
let array = [];
for(let i = 20; i <= 800; i++) {
if(i % 4 == 0) {
array.push(i);
}
}
console.log(array);
This question already has answers here:
How to convert array into comma separated string in javascript [duplicate]
(3 answers)
Closed 2 years ago.
I want to add a % after each element in the array except the last. So far I have come up with this:
var array = [a, b, c];
for(var i=0; i<array.length; i++) {
var outcome += array[i] + '%';
}
Outcome:
a%b%c%
How can I fix this so the % does not appear at the end of the outcome?
You can use the Array.prototype.join method in order to get what you're after:
console.info(['a', 'b', 'c'].join('%'))
Check if the current element (value of i) is not the last element. If it's the last element don't concatenate a %, for all others concatenate with the %.
for(var i = 0; i < arr.length; i++) {
if(arr[i] < arr.length -1) {
var outcome += arr[i] + '%';
}
}
This question already has answers here:
Using Javascript to compare two input numbers in HTML?
(4 answers)
issue with comparing two numbers in javascript
(5 answers)
compare two input values in input validation html javascript?
(3 answers)
Closed 3 years ago.
I'm writing my first javascript/html code to collect football results and then produce a league table. My code copes with single digit scores fine but not double digit scores.
Three arrays are used for results, league and players as below:
Results=[];
var Results=[
["Home","F","A","Away"],
["Si",,,"Ste"],
["Si",,,"Gaz"],
["Ste",,,"Gaz"],
["Ste",,,"Si"],
["Gaz",,,"Si"],
["Gaz",,,"Ste"],
["Si",,,"Ste"],
["Si",,,"Gaz"],
["Ste",,,"Gaz"],
["Ste",,,"Si"],
["Gaz",,,"Si"],
["Gaz",,,"Ste"],
];
League=[];
var League=[
["Team","P","W","D","L","F","A","GD","Pts"],
["Si",,,,,,,,],
["Ste",,,,,,,,],
["Gaz",,,,,,,,]
];
players=[];
var players=["Si","Ste","Gaz"];
I believe the faulty code is where wins are calculated:
if (Results[i][1]>Results[i][2])
{ wins++;
pts= +pts + 3;
}
This is taken from two 'for' loops that iterate through the results array for each player and populate the league array accordingly:
for (p = 0; p < players.length; p++)
{
for (i = 1; i < Results.length; i++)
{
if (Results[i][1]!= "")
{
if (Results[i][0]==players[p])
{
pld++;
goalsF=+goalsF + +Results[i][1];
goalsA=+goalsA + +Results[i][2];
gd=(goalsF - goalsA);
if (Results[i][1]>Results[i][2])
{
wins++;
pts= +pts + 3;
}
else if (Results[i][1]<Results[i][2])
{
loses++;
}
else
{
draws++;
pts++
}
}
}
if (Results[i][1]!= "")
{
if (Results[i][3]==players[p])
{
pld++;
goalsF=+goalsF + +Results[i][2];
goalsA=+goalsA + +Results[i][1];
gd=(goalsF - goalsA);
if (Results[i][1]<Results[i][2])
{
wins++;
pts= +pts + 3;
}
else if (Results[i][1]>Results[i][2])
{
loses++;
}
else
{
draws++;
pts++
}
}
}
}
r=p+1;
League[r][1]=pld;
League[r][2]=wins;
League[r][3]=draws;
League[r][4]=loses;
League[r][5]=goalsF;
League[r][6]=goalsA;
League[r][7]=gd;
League[r][8]=pts;
var pld=0;
var wins=0;
var draws=0;
var loses=0;
var goalsF=0;
var goalsA=0;
var gd=0;
var pts=0;
var r=0;
}
For a 10-1 score it works correctly:
For 10-2 (or 10-3) the result gets reversed and the player with the lower score is deemed the winner!?
It's as if the comparison is only working on the first digit of the scoreline. How do I fix this?