Skip to content

Instantly share code, notes, and snippets.

@joseederangojr
Last active October 27, 2020 06:50
Show Gist options
  • Save joseederangojr/fe39408f586501b971182613c7392362 to your computer and use it in GitHub Desktop.
Save joseederangojr/fe39408f586501b971182613c7392362 to your computer and use it in GitHub Desktop.
A simple programming contest for our college
/**
* @description REQUIRED!!
* Create a function to tests if given
* parameters are equal
* @note
* this function with be used to test your outputs :D
* if you fail to create this function
* you're score will be 0 automatically
*/
const assertEqual = (output, expected) => output === expected
/**
* @description
* Create a function/method that takes a text
* and transform it to uppercase
* @example
* assertEqual(toUppercase("hello"), "HELLO")
* assertEqual(toUppercase("Yes"), "YES")
* assertEqual(toUppercase("cyb3r7unk"), "CYB3R7UNK")
*/
const toUppercase = text => text.toUpperCase()
/**
* @description
* Create a function/method that takes a text
* and count how many characters from a given text
* and also ignores spaces
* @example
* assertEqual(countCharacters("hello"), 5)
* assertEqual(countCharacters(" foo bar "), 6)
* assertEqual(countCharacters(" with_spaces "), 11)
*/
const countCharacters = text => text.replace(/ /g, '').length
/**
* @description
* Create a function/method that takes a text
* and count how many word in the text
* and also ignore extra spaces
* @example
* assertEqual(countWords("Hello world"), 2)
* assertEqual(countWords(" iuyhnuh 198nue 08nh 87r1h1ygeh a "), 5)
* assertEqual(countWords("A ____ hello yes -> haosihdkj"), 6)
*/
const countWords = text => text.replace(/\s+/g, ' ').trim().split(" ").length
/**
* @description
* Create a function/method that takes a text and reverse it
* @example
* assertEqual(reverseWord("hello"), "olleh")
* assertEqual(reverseWord("yes++"), "++sey")
* assertEqual(reverseWord(""), "")
*/
const reverseWord = text => [...text].reverse().join("")
/**
* @description
* Create a function/method that takes a number
* and determines if its a odd number
* @example
* assertEqual(isOdd(2), true)
* assertEqual(isOdd(3), false)
* assertEqual(isOdd(4), true)
*/
const isOdd = num => !(num % 2)
/**
* @description
* Create a function/method that takes a number
* that determine if its a perfect square number
* @example
* assertEqual(isPerfectSquare(2), false)
* assertEqual(isPerfectSquare(3), false)
* assertEqual(isPerfectSquare(16), true)
*/
const isPerfectSquare = num => !(parseInt(num / 2) % 2)
/**
* @description
* create a function/methods that takes a text
* and count how many times character is duplicated
* this includes spaces
* @note case sensitive
* @example
* assertEqual(countDuplicates("hello world "), 4)
* assertEqual(countDuplicates("nani?"), 1)
* assertEqual(countDuplicates("1cn ehync ur1uhihnf hnho87v1yr87v1y hnu"), 24)
*/
const countDuplicates = text => text.length - [...new Set(text)].length
/**
* @description
* Create a function/method that takes a text
* and count how many numbers are in the text
* @example
* assertEqual(countNumbers("cyb3r7unk"), 2)
* assertEqual(countNumbers("1asu1n982ecu1c1jnem"), 7)
* assertEqual(countNumbers("8cby13n81h1n2ubyr218n1c"), 11)
*/
const countNumbers = text => [...text].filter(char => !isNaN(char)).length
/**
* @description
* Create a function/method that will take a text
* and for every odd index transform the character to uppercase
* @note use your isOdd and toUppercase solutions to create this function
* @example
* assertEqual(camelCase("hello"), "hElLo")
* assertEqual(camelCase("world"), "wOrLd")
* assertEqual(camelCase("foo bar"), "fOo bAr")
*/
const camelCase = text => [...text].map((char, index) => isOdd(index + 1) ? toUppercase(char) : char).join("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment