Skip to content

Instantly share code, notes, and snippets.

//test cases
let tests = [
{
input: 3,
result: 'Please input an array of integers.'
},
{
input: '[]',
result: 'Please input an array of integers.'
},
function countCharOccurances (text, character) {
// convert input to string and split the string into an array of charaters
let chars = String(text).split("")
// if doesn't include, return 0
if (!chars.includes(String(character))) {
return 0
} else {
let count = 0
function firstUniqueCharacter(text) {
// split string into single characters
let characters = String(text).split("")
if (characters.length < 1) return 'Please input a non-empty string'
// iterate through array, comparing each character to the rest
// if found no match for a certain character after comparing to all other characters, return character at the end of first such iteration
function swapWithoutThirdVariable({ firstNo, secondNo }) {
// if either of two input numbers don't pass as integers, return error
if (!Number.isInteger(firstNo) || !Number.isInteger(secondNo)) {
return 'Please input two integers for both "firstNo" and "secondNo"'
}
// first number is sum of two numbers
firstNo = firstNo + secondNo
// get first number again afer subtracting the second number from sum (firstNo)
// validate input array elements
function isIntegerArray (input) {
// if an array
if (Array.isArray(input)) {
// if empty array
if (input.length < 1) return false
// iterate through each element and return false if one of them happens to be not an integer
for (let i = 0; i < input.length; i++) {
if (Number.isInteger(input[i])) continue
// validate input array elements
function isIntegerArray (input) {
// if an array
if (Array.isArray(input)) {
// if empty array
if (input.length < 1) return false
// iterate through each element and return false if one of them happens to be not an integer
for (let i = 0; i < input.length; i++) {
if (Number.isInteger(input[i])) continue
// validate input array elements
function isIntegerArray (input) {
// if an array
if (Array.isArray(input)) {
// if empty array
if (input.length < 1) return false
// iterate through each element and return false if one of them happens to be not an integer
for (let i = 0; i < input.length; i++) {
if (Number.isInteger(input[i])) continue
// validate input array elements
function isIntegerArray (input) {
// if an array
if (Array.isArray(input)) {
// if empty array
if (input.length < 1) return false
// iterate through each element and return false if one of them happens to be not an integer
for (let i = 0; i < input.length; i++) {
if (Number.isInteger(input[i])) continue
// assumption - each input would have exactly one solution, and the same element may not be used twice in the input array
// brute force solution
function twoSumNaive(array, sum) {
let indices = []
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] + array[j] === sum) {
indices.push(i)
@fazeelanizam13
fazeelanizam13 / javascript-hash-table.js
Created May 21, 2022 15:00
Implementation of the hash table data structure in JavaScript.
const hashFunction = (key, tableSize) => {
// index we are going to return
let index = 0
// get list of characters in key string as an array
let numChars = String(key)
// assign numChars size to index to start
index = numChars.length