Code Samples
Here is a collection of commonly asked coding questions. Each question has an explanation of how to perform the operation specified as well as a working code sample that can copied. These examples aim to provide the most pragmatic and succinct code that optimizes efficiency and readability. We'll be constantly adding examples and if you have questions or requests, contact us on FB or TW.
The standard way to sort an array of objects is to use the sort method and specify a custom compare function. The compare function can then be used to specify custom code that will be used when comparing objects to determine the order. The compare function takes two parameters a
and b
. a
and b
represent the first and second element to be compared. The compare function should return an integer and if the integer is > 0
then a
should be sorted after b
. If the integer returned is < 0
then a
should be sorted before b
. And finally if 0
is returned then a
and b
are equivalent and can stay in their original order.
Read more > The standard way to remove an item from a list is to use remove by specifying the item you want removed. This will remove the first occurrence of the item specified in the list. One thing to note is that if the item specified does not exist, a ValueError will be raised. You can also use del to remove an item from the list by specifying the index.
Read more > The standard way to remove an item from an array is to use indexOf to find the position of the item and then use splice to remove the item from the array.
Read more > The standard way to get all the key/values of an object are to iterate over the objects's keys using Object.keys(). Once you have all they keys of an object, you can simply get the corresponding values and do whatever you like with them.
Read more > There are multiple ways to insert an item into a list in Python depending on your use-case. The first is append
which will add the item to the end of the list. There's also insert
to add the item at an arbitrary index. And if you need to add a list of items to a list, you can use extend
. And in addition to extend
, you can also use +
to concatenate multiple lists together.
Read more > The standard way to insert an item into an array is to use splice and provide the item your want to insert as the third argument. The first argument is the specific index where you want to insert the item and the second argument should be set to 0
since we're not removing anything from the array.
Read more > OpenAI has made it really easy to get started using ChatGPT and provides documentation and libraries for a wide variety of languages (Python, Javacript, C#/.NET, Crystal, Go, Java, Kotlin, PHP, R, Ruby, Scala, Swift, Unity, Unreal Engine). You'll need to signup for an account and then create an API key. Once you have an API key, you can start making requests to ChatGPT and then the possibilities are limitless as far as what you can do with it.
Read more > OpenAI has made it really easy to get started using ChatGPT and provides documentation and libraries for a wide variety of languages (Python, Javacript, C#/.NET, Crystal, Go, Java, Kotlin, PHP, R, Ruby, Scala, Swift, Unity, Unreal Engine). You'll need to signup for an account and then create an API key. Once you have an API key, you can start making requests to ChatGPT and then the possibilities are limitless as far as what you can do with it.
Read more > The standard way to get the current date/time is to use the datetime.today method from the datetime module which will return the current local date and time. You can then access year
, month
, day
, hour
, minute
, second
, microsecond
and tzinfo
. You can also use strftime to format the datetime
object into any string format.
Read more > The standard way to get the current date/time is to use new Date() which will return a new date object set to the current date/time. You can then manipulate the object to retrieve specific properties of a date such as day, month, year, hour, minute, etc.
Read more > The standard way to generate a random number is to use the random module. The random()
function will return a floating point number from 0 to 1 (but not including 1). In addition, there's also a choice()
function that will randomly select an item from a list. choice()
can be used to select from a pre-specified list of characters or numbers.
Read more > The standard way to generate a random number is to use Math.random() which will return a floating-point, pseudo-random number between 0 and 1 (but not including 1). It should be noted that Math.random()
does not provide a cryptographically secure random number and should not be used for anything related to security. An alternative to Math.random()
is Cyrpto.getRandomValues() which provides cryptographically strong random values.
Read more > The most straightforward way to check if a string contains a substring is to use in. You can also use find or index to get the position of the substring you're looking for. If the substring doesn't exist, you'll get a -1
.
Read more > The most common (and supported) way to check if a string contains a substring is to use indexOf. You can also use includes but it's only supported on newer browsers that support ECMAScript 6 (ES6). Both do a case-sensitive search so if you're looking for a case-insensitive search, you would want to run toLowerCase on both the input and search param.
Read more > The standard way to append an item to an array is to use Array.push. The method push()
allows you to append one or multiple items to an array. In addition, you can also use Array.concat to append one array to another array.
Read more >