Skip to content
Advertisement

Tag: push

jQuery sort() is not working in function()

I am trying to sort the array after adding new entry into the array using push(). However there are 2 errors. First the sort() is not allowed. Second is when I remove the sort() the new entry always showed with a number of 5 attached Answer When adding new object into an array, you only need to use arr.push(obj), and

Javascript – push String to Array returns Integer

I add some strings in an array. Why I receive number 2 in the second log when name is a String too? I tried also let arr2 = arr1.slice().push(name); without success. Answer arr.push() modifies the arr itself and returns the length of the resulting array, to do what you want to do, you can do one of the two following

Create a string of specified copies of a given string

I am trying to iterate over the original string 3 times. The result I get is: [“a”,”b”,”c”,”d”,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined] The correct result should be: [“a”, “b”, “c”, “d”, “a”, “b”, “c”, “d”, “a”, “b”, “c”, “d”] I have tried many variations but nothing works, this is the closest I got. Answer JavaScript has a repeat Method on Strings. You can just use

How to add 5 random numbers which are less than 10?

I’ve created two functions. One to create 5 random numbers to push them into an array. And another one to sum up the numbers. The random number generator is working and making an array perfectly. But the sum is not accurate. I’m unable to find where the problem is. Answer Because you are logging a different set of array values

Advertisement