I have a problem with my exercise:
const negativeNumbers = [];
function extractNegativeNumbers(numbers) {
if (numbers<0){
console.log("It's negative number")
} else {
console.log("It's not negative number")
}
}
I don't know how to append my function's result to const array. Which operator should I use to do this ?
I have a problem with my exercise:
const negativeNumbers = [];
function extractNegativeNumbers(numbers) {
if (numbers<0){
console.log("It's negative number")
} else {
console.log("It's not negative number")
}
}
I don't know how to append my function's result to const array. Which operator should I use to do this ?
Share Improve this question edited May 1, 2020 at 23:15 Lajos Arpad 77.6k41 gold badges117 silver badges223 bronze badges asked May 1, 2020 at 23:00 sepsonsepson 131 silver badge4 bronze badges 3- Hey sepson what is the expected argument to your functtion it's an array right?! – Saadi Toumi Fouad Commented May 1, 2020 at 23:34
- const negativeNumbers = []; function extractNegativeNumbers(numbers) { // append any negative numbers found in the numbers array // into the negativeNumbers array constant variable above } i have something like this as an exercise – sepson Commented May 1, 2020 at 23:37
- I knew it from the beginning but I just wanted to be sure, happy coding! – Saadi Toumi Fouad Commented May 1, 2020 at 23:39
4 Answers
Reset to default 6You should use Array.prototype.push
Edit: If your numbers
parameter is an array rather than a single value, then use the following to check each item and append it:
const negativeNumbers = [];
function extractNegativeNumbers(numbers) {
numbers.forEach(num => {
if (num < 0) {
console.log("It's negative number");
negativeNumbers.push(num);
} else console.log("It's not negative number");
});
}
You don't use an operator in this case, instead you use the built-in function push which belongs to the Array
object.
Some of the others mentioned using let
or var
instead, but neither is necessary because array objects are mutable. What this means is that updating the items of an array doesn't actually change the value of the variable, as the variable is still set to the same array object (regardless of what the array contains). In this case, all that adding the const
keyword does is prevent you from reassigning the variable negativeNumbers
to a different value
Your function does not make sense. If it's to receive an array called numbers and append to your const array, then it should be:
function extractNegativeNumbers(numbers) {
for (let num of numbers)
if (num < 0)
negativeNumbers.push(num);
}
Note, that even though your array is constant, its items can be changed. const
prevents you from reassigning values for your variable, but not from adding items to your array. Also, if negativeNumbers.push(num);
is not to your taste, you can use negativeNumbers[negativeNumbers.length] = num;
instead.
Declare the array using let as const is used to declare constants.
let negativeNumbers = [];
You can use push function to add elements.
negativeNumbers.push(number +' is a negative number');
For further reference: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Here is an example on how to do it
const numbers = [13, 3, -4, 78, -14, 12, -8],
negativeNumbers = numbers.filter(n => n < 0);
console.log(negativeNumbers);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745168521a4614781.html
评论列表(0条)