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
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():
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)
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. .
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
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:
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()
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)
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, 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?