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));
Related
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Count unique elements in array without sorting
(9 answers)
Closed 14 days ago.
have made some code but it is broken and gives errors. the code is supposed to find the number of unique numerical elements in any array. So far, i created the following code snippet which contains a uniqueLength() function to return the number of unique elements.
function uniqueLength(nums) => {
if(nums.length === 0) {
return 0;
}
let i=0;
while(j>nums.lengths) {
if(nums[j]] ==! nums[i]) {
i++;
nums[i] = nums[j];
j++;
} else {
j++;
}
}
give i+1;
}
// Should return 5
const result = uniqueLength([1,1,2,3,4,5,5]);
console.log(result);
// Should return 1
const result2 = uniqueLength([1,1,1,1]);
console.log(result2);
where there is dashes one of these hints should go there
return
nums[j]
!=
nums[j] == nums[i]
nums[i]
give
j > nums.length
returns
let j=1;
=
**!==
j < nums.length**
This question already has answers here:
How to compute the sum and average of elements in an array? [duplicate]
(35 answers)
Closed 10 months ago.
I tried to iterate through the array and print the sum. But the output am getting is elements of the array.
<p id="ans"></p>
<script>
var text = "the mean is ";
function mean() {
var sum = 0;
var input = document.getElementsByName("values");
for (var i = 0; i < input.length; i++) {
sum += input[i].value;
text = text + sum;
}
document.getElementById("ans").innerHTML = text;
}
</script>
Parse the string values into integers and then sum it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
This is because in javascript if you adding a number to a string it will be casted to a string. For example:
0 + '1' + '2' // 012
But with parse int:
0 + parseInt('1') + parseInt('2') // 3
Or you can cast to int with a simple plus also:
0 + (+'1') + (+'2') // 3
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:
How do you check that a number is NaN in JavaScript?
(33 answers)
Closed 5 years ago.
I'm trying to check to see if an input is NaN, basically I want to alert 0 if nums doesn't have anything in it, or if that something is a 0. Any ideas on how to go about this?
var sumofnums = 0,
nums = document.getElementById("nums").value.split(",");
function add(){
for (i = 0; i < nums.length; i++) {
sumofnums += parseInt(nums[i]);
};
document.getElementById("sum").innerHTML = sumofnums;
};
if (nums === ''){
alert('0');
}
isNaN();
Google can help as well.
This question already has answers here:
How to find the sum of an array of numbers
(59 answers)
Closed 6 years ago.
I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work:
var total = 0;
for (var i = 0; i < totalPrice.length; i++);
total += totalPrice[i];
document.getElementById("displayPrice").innerHTML = total;
But total comes out as a NaN.
This is an example of my code in JSFiddle, if you add the item twice the value gets pushed into the array, what am I doing wrong with the for loop?
https://jsfiddle.net/bgv5s9re/2/
You could use brackets
var total = 0;
for (var i = 0; i < totalPrice.length; i++){
total += totalPrice[i];
}
document.getElementById("displayPrice").innerHTML = total;