Created
December 14, 2020 10:11
-
-
Save Rooarii/5a027f940ba99b7e4cda0e38db47859c to your computer and use it in GitHub Desktop.
TDD en JavaScript 1 - Introduction
This file contains hidden or 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
const assert = require('assert') | |
// Type your code here | |
const capitalizeFirstLetters =(input)=>{ | |
// return input.length && !input.includes(' ') > 0 ? input[0].toUpperCase() + input.slice(1): ''; | |
// first code version | |
let output = ''; | |
if (input.length >0 && !input.includes(' ') ){ | |
for (let i = 0 ; i < input.length ; i++) { | |
if (i === 0) { | |
output += input[0].toUpperCase(); | |
} else { | |
output += input[i]; | |
} | |
} | |
}else if (input.length >0 && input.includes(' ')){ | |
let inputSplit=input .split(' '); | |
outputSplit = inputSplit.map(e =>( | |
e[0].toUpperCase()+ e.slice(1)) | |
); | |
output = outputSplit.join(' '); | |
console.log(output) | |
} | |
else { | |
output ='' | |
} | |
return output | |
} | |
// ************* tests | |
// test1: check that capitalizeFirstLetters is a function | |
assert.strictEqual(typeof capitalizeFirstLetters, 'function'); | |
// test2: check that capitalizeFirstLetters takes one argument | |
assert.strictEqual(capitalizeFirstLetters.length, 1) | |
// // test3: check that capitalizeFirstLetters argument is a string | |
// assert.strictEqual(typeof input, 'string'); | |
// test4: Check that capitalizeFirstLetters transforms javaScript correctly | |
assert.strictEqual(capitalizeFirstLetters('javaScript'), 'JavaScript'); | |
// test5: Check that it works for a 1-character string | |
assert.strictEqual(capitalizeFirstLetters('z'), 'Z'); | |
// test6: Check that it works for i am learning TDD | |
assert.strictEqual(capitalizeFirstLetters('i am learning TDD'), 'I Am Learning TDD'); | |
// test7: Check that it works for an empty string | |
assert.strictEqual(capitalizeFirstLetters(''), ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment