Last active
January 25, 2020 19:23
-
-
Save coderwurst/eb476a8f8f6e2c67a25617792c3884f9 to your computer and use it in GitHub Desktop.
String examples written in JavaScript
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
var string = "this is a nice string" | |
var arrayString = [ ...string.split('')] | |
arrayString | |
(21) ["t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "n", "i", "c", "e", " ", "s", "t", "r", "i", "n", "g"] | |
arrayString.reverse() | |
(21) ["g", "n", "i", "r", "t", "s", " ", "e", "c", "i", "n", " ", "a", " ", "s", "i", " ", "s", "i", "h", "t"] | |
var newString = arrayString.toString() | |
console.log(newString) | |
// 1 g,n,i,r,t,s, ,e,c,i,n, ,a, ,s,i, ,s,i,h,t | |
var reversed = newString.replace(/,/g , " "); | |
console.log(reversed) | |
// 1 g n i r t s e c i n a s i s i h t | |
/** | |
** Algorithm Question - Test if string has all unique chars | |
**/ | |
// solution I - remove all duplicates and compare with original string | |
{ | |
console.log("Solution I"); | |
const testStringFalse = "let us go where no one goes"; | |
const testStringTrue = "abcdefghijklmnopqrstuvwxyz"; | |
function uniqueChars(string) { | |
return [ ...new Set(string)]; | |
} | |
function isUnique(string) { | |
return uniqueChars(string).length === string.length; | |
} | |
console.log(isUnique(testStringFalse)); | |
console.log(isUnique(testStringTrue)); | |
} | |
// solution II - array of booleans to store if ascii char already found | |
{ | |
console.log("Solution II"); | |
const testStringFalse = "let us go where no one goes"; | |
const testStringTrue = "abcdefghijklmnopqrstuvwxyz1234567890"; | |
function uniqueChars(string) { | |
if (string.length > 128) return false; // ascii original exceeded | |
var charSet = new Array(128); | |
for(var i = 0; i < string.length; i++) { | |
var value = string.charAt(i); | |
if(charSet[value]) return false; | |
charSet[value] = true; | |
} | |
return true; | |
} | |
console.log(uniqueChars(testStringFalse)); | |
console.log(uniqueChars(testStringTrue)); | |
} | |
// solution III compare each char with rest of string | |
{ | |
console.log("Solution III"); | |
const testStringFalse = "let us go where no one goes"; | |
const testStringTrue = "abcdefghijklmnopqrstuvwxyz1234567890"; | |
function uniqueChars(string) { | |
for(var i = 0; i < string.length; i++) { | |
var value = string.charAt(i); | |
for (var j = i + 1 ; j < string.length; j++) { | |
var valueTwo = string.charAt(j); | |
console.log('valueOne:' + value + ' compared to: ' + valueTwo); | |
if(value == valueTwo) return false; | |
} | |
} | |
return true; | |
} | |
console.log(uniqueChars(testStringFalse)); | |
console.log(uniqueChars(testStringTrue)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment