How to add an element to an array?

I can’t add an element to the array, the logic is that when generating a random number, it must be checked using the myNumbers variable, if there is no such number, then add it to the arrayPreformatted text

const MIN = 1000
const MAX = 9999

const myNumbers = [2355, 7235, 8135, 1762, 2361, 8351]
console.log(myNumbers)
const arr = Math.ceil(Math.random() * (MIN - MAX) + 10000)
console.log(arr)
function mat(){
if(arr !== myNumbers){
 const uns = myNumbers.unshift()
 console.log(uns)
}

}
mat()

Hi @pinkod02, your arr is a number not an array, and a number is never equal to an array (*)… to check if an array contains a specific element though, use the includes() method. Also, you need to pass the element you want to add to unshift():

const MIN = 1000
const MAX = 9999

const myNumbers = [2355, 7235, 8135, 1762, 2361, 8351]
const randomNumber = Math.ceil(Math.random() * (MIN - MAX) + 10000)

if (!myNumber.includes(randomNumber) {
  myNumbers.unshift(randomNumber)
}

*) In fact, an array is only ever equal to itself, i.e. if it refers to the same object:

const a = [1, 2, 3]
const b = [1, 2, 3]
const c = a

console.log(a === a) // true
console.log(a === b) // false
console.log(a === c) // true
2 Likes

I realized that I need to make a number into an array, but how to add a non-existing number to myNumbers, to add a new value to the array
myNumbers.unshift(arr)

The full solution is literally in post #2. What made you think you needed to do something differently?

1 Like

yes, I would like to add numbers to the array with each update, that is, so that the array increases in values

See post #2. That is how you do it.

this is not exactly what I want, #2 it adds values to the array and changes it, but I want more values to be added, i.e. if there were 8 values, it became 9 then 10, etc. .

Okay, so add a for loop.

MDN can tell you all about it https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

Indeed, I omitted the mat() function wrapper… you’d want to generate the random number inside that function then though. Otherwise you’d check for the same number in each call, which would thus only get added the first time (if at all).

It does nothing for me, since I have a different form of writing, and I know what you are suggesting, but none of you can tell me how to write the code correctly, so I don’t understand anything

const MIN = 1000
const MAX = 9999

const myNumbers = [2355, 7235, 8135, 1762, 2361, 8351]
console.log('оригинал: '+ myNumbers)
 const arr = Math.ceil(Math.random() * (MIN - MAX) + 10000)

function mat(){
let add = myNumbers.forEach()
if(add !== arr){
  myNumbers.push()
}
if(!myNumbers.includes(arr)){
 const uns = myNumbers.unshift(arr)

 
 console.log(uns)
 console.log(myNumbers)
}

}
mat()

I did this but it doesn’t work, the array doesn’t grow

This will just throw an error as forEach() expects a callback function to be passed… what’s the purpose of that assignment BTW? Calling forEach() will just return undefined, check out the MDN docs for a description and examples:

1 Like

forEach() is a loop that iterates all the way to the end, only in my task it doesn’t work, because when I write the code it doesn’t work, how to write the code correctly?

function add(){
  const uns1 = myNumbers.unshift(arr)
  const w = uns1.forEach()
  if(w !== myNumbers){
const res = arr.push(myNumbers)
console.log(res)
  }
  
}
add()

Well again, forEach() expects a callback function that will get called for each element in the array… for example:

myNumbers.forEach(function (element) {
  console.log(element)
})

// Logs:
// 2355
// 7235
// 8135
// 1762
// 2361
// 8351

Your code is missing such a callback, so it will throw an error and not run to completion.

yes, but how to add another value to the array? my code is not working.`function mat(){

if(!myNumbers.includes(arr)){
const uns = myNumbers.unshift(arr)
console.log(uns)

}
uns.forEach(function(el) {
uns.push(el)
uns++

console.log(uns)
})

}
mat()`

Here you are assigning a number to uns as you should see in the console.

Here you seem to expect uns to be an array… this will also throw an error, since numbers can’t be iterated over and don’t have a forEach() method.

Edit: Actually, that const uns assignment would be scoped to the if block, and outside of that block it’s not even defined.

Of course, I thought that it was very easy to do such elementary things, but in fact I understand that this is impossible, of course I am completely confused, since such difficulties cannot be understood, it is easier to close this topic than to figure it out)

Well just try a plain for loop as suggested by @rpkamp:

const MIN = 1000
const MAX = 9999

const myNumbers = [2355, 7235, 8135, 1762, 2361, 8351]
const maxNumbersToAdd = myNumbers.length

for (let i = 0; i < maxNumbersToAdd; i++) {
  const randomNumber = Math.ceil(Math.random() * (MIN - MAX) + 10000)
  
  if (!myNumbers.includes(randomNumber)) {
    myNumbers.unshift(randomNumber)
  }
}

Sorry BTW I just noticed a missing closing parenthesis in my first reply… :-$ HTH now.

It is not clear at all, if const maxNumbersToAdd = myNumbers.length, then why 12 elements are displayed in console.log(myNumbers)? I thought if const maxNumbersToAdd = myNumbers.length shows 6 elements, then the array should increase by the order of 7,8, 9, and so on, but it’s not clear why 12 elements appeared at once, and then does not increase?

So, three stages of thinking to answer:


So, if maxNumbersToAdd is equal to myNumbers.length, and myNumbers starts off as [2355, 7235, 8135, 1762, 2361, 8351], what is the value of maxNumbersToAdd?


for (let i = 0; i < maxNumbersToAdd; i++) {

Once you know maxNumbersToAdd, how many numbers are being added to the array, if you loop this many times?


If you add that many numbers to myNumbers, what will the total number of things in myNumbers be after the script is done?

1 Like