Created
March 29, 2021 12:06
-
-
Save FelixLuciano/292e74be3625d16ab8fad68907cb74b0 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo #189 - Interview question of the week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* From: https://buttondown.email/cassidoo/archive/success-is-more-a-function-of-consistent-common/ | |
* @param {string} str String | |
* @returns {boolean} If the given string represents a valid number. | |
* @example | |
* $ validNumber("-123.456") | |
* -> true | |
* | |
* $ validNumber("abc123") | |
* -> false | |
*/ | |
function validNumber (str) { | |
let isFloat = false | |
for (let i = 0; i < str.length; i++) { | |
const char = str[i] | |
if (i === 0 && strIncludes("+-.", char)) | |
if (str.length < 2) return false | |
else continue | |
else if (char === ".") | |
if (!isFloat) isFloat = true | |
else return false | |
else if (!strIncludes("0123456789", char)) return false | |
} | |
return true | |
} | |
/** | |
* @param {string} dictionary | |
* @param {string} character | |
* @returns {boolean} If the `dictionary` contains the given `character`. | |
* @example | |
* $ validNumber("abc123", "b") | |
* -> true | |
* | |
* $ validNumber("def456", "g") | |
* -> false | |
*/ | |
function strIncludes (dictionary, character) { | |
for (let item of dictionary) | |
if (character === item) return true | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment