Created
April 24, 2020 03:57
-
-
Save leighlars/8ce968c5a22299161879f153f9ba08a6 to your computer and use it in GitHub Desktop.
Repeat Practice, Kata 8 on Codewars
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
###Description | |
Over the Intermission break, I worked on CodeWars, kata 8, to practice pseudo-code skills, and gain exposure to different ways of coding. | |
####1. String Repeat | |
Instructions: Write a function named repeatString that repeats the string (str), "n" times. | |
Sample tests: | |
``` | |
describe("Tests", function() { | |
it ("Basic tests", function() { | |
Test.assertSimilar(repeatStr(3, "*"), "***"); | |
Test.assertSimilar(repeatStr(5, "#"), "#####"); | |
Test.assertSimilar(repeatStr(2, "ha "), "ha ha "); | |
}); | |
}); | |
```` | |
Pseudocode: | |
Write a function named repeatString that repeats the string (str), n times. | |
``` | |
repeatString() | |
`` | |
string and n will be placeholders | |
repeatString(n, str) { | |
(instance of string) * n; <---- research string.protoypes, assign to variable | |
return "str repeated by n" <----- return variable | |
} | |
repeatString(n, str) { | |
var repeatedString = str.repeat(n); | |
return repeatedString | |
} | |
``` | |
#### 2. Century From Year | |
The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc. Given a year, return the century it is in. | |
Sample tests | |
``` | |
Test.assertEquals(century(1705), 18, 'Testing for year 1705'); | |
Test.assertEquals(century(1900), 19, 'Testing for year 1900'); | |
Test.assertEquals(century(1601), 17, 'Testing for year 1601'); | |
Test.assertEquals(century(2000), 20, 'Testing for year 2000'); | |
Test.assertEquals(century(89), 1, 'Testing for year 89'); | |
``` | |
Psuedocode: | |
write function with year as parameter | |
year - 1 | |
century is year/100 | |
round down | |
add 1 | |
``` | |
function century(year) { | |
century = Math.floor((year-1)/100) + 1; | |
return `${century}, Testing for year ${year}`; | |
} | |
``` | |
#### 3. Abbreviate a 2-Word Name | |
Write a function to convert a name into initials. This kata strictly takes two words with one space in between them. | |
The output should be two capital letters with a dot separating them. | |
Sample tests: | |
``` | |
Test.assertEquals(abbrevName("Sam Harris"), "S.H"); | |
Test.assertEquals(abbrevName("Patrick Feenan"), "P.F"); | |
Test.assertEquals(abbrevName("Evan Cole"), "E.C"); | |
Test.assertEquals(abbrevName("P Favuzzi"), "P.F"); | |
Test.assertEquals(abbrevName("David Mendieta"), "D.M"); | |
``` | |
Pseudocode: | |
Write function shell and pass in 2-word name. | |
function abbrevName("Two Words") { | |
get each word; <--- research string.proto() | |
var getIndivWord = string.split(); | |
get first char of each word | |
string.charAt(0); | |
capitalize the chars | |
Add "." to each char | |
return interpolate "1stchar" + "." + 2ndChar" + "." | |
} | |
Coded: | |
function abbrevName(name) { | |
var splitName = name.split(" "); | |
var firstName = splitName[0]; | |
var secondName = splitName[1] | |
var firstInit = firstName.charAt(0).toUpperCase(); | |
var secInit = secondName.charAt(0).toUpperCase(); | |
return `${firstInit}.${secInit}` | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment