diff --git a/staff/carlos-bock/chain-characters/index.js b/staff/carlos-bock/chain-characters/index.js index 2e6995fc..c1d26bc5 100644 --- a/staff/carlos-bock/chain-characters/index.js +++ b/staff/carlos-bock/chain-characters/index.js @@ -5,8 +5,9 @@ const concat = require("./concat.js") const endsWith = require("./endsWith.js") const includes = require("./includes.js") const indexOf = require("./indexOf.js") +const replace = require("./replace.js") const repeat = require("./repeat.js") - +const slice = require("./slice.js") const split = require("./split.js") @@ -22,7 +23,9 @@ ChainCharacters.prototype.concat = concat; ChainCharacters.prototype.endsWith = endsWith; ChainCharacters.prototype.includes = includes; ChainCharacters.prototype.indexOf = indexOf; +ChainCharacters.prototype.replace = replace; ChainCharacters.prototype.repeat = repeat; +ChainCharacters.prototype.slice = slice; ChainCharacters.prototype.split = split; diff --git a/staff/carlos-bock/chain-characters/index.test.js b/staff/carlos-bock/chain-characters/index.test.js index 3195dc9f..7f74f487 100644 --- a/staff/carlos-bock/chain-characters/index.test.js +++ b/staff/carlos-bock/chain-characters/index.test.js @@ -4,8 +4,9 @@ require("./concat.test.js"); require("./endsWith.test.js"); require("./includes.test.js"); require("./indexOf.test.js"); +require("./replace.test.js"); require("./repeat.test.js"); - +require("./slice.test.js"); require("./split.test.js"); diff --git a/staff/carlos-bock/chain-characters/replace.js b/staff/carlos-bock/chain-characters/replace.js index b76f2aca..352e6e0f 100644 --- a/staff/carlos-bock/chain-characters/replace.js +++ b/staff/carlos-bock/chain-characters/replace.js @@ -6,9 +6,10 @@ //si parametro === a string[i] entonces newString = string[string.length-index] + parametro + string[segunda mitad] //resolver primero string al principio +const ChainCharacters = require("./constructor"); -function replace(string, oldPhrase, newPhrase) { - const oldString = string; +function replace(oldPhrase, newPhrase) { //removed string parameter + const oldString = this.value; let result = false; let newString =""; @@ -19,7 +20,7 @@ function replace(string, oldPhrase, newPhrase) { let solution = true; for (let j = 0; j < oldPhrase.length; j++) { - if (string[i + j] !== oldPhrase[j]){ + if (this.value[i + j] !== oldPhrase[j]){ solution = false; break; } @@ -31,29 +32,11 @@ function replace(string, oldPhrase, newPhrase) { i += oldPhrase.length - 1; result = true; } else { - newString += string[i]; + newString += this.value[i]; } } - return newString; + return new ChainCharacters(newString); } -const aString = "My favorite color is blue"; -const oldWord = "blue"; -const newWord = "red"; - -const stringB = "Cual es el mejor bootcamp"; -const pregunta = "Cual"; -const respuesta = "ISDI"; - -const result1 = replace(aString, oldWord, newWord); -console.assert(result1 === aString.replace(oldWord, newWord),{ - result: result1, - message: "Test 1 No pasado ", -}); - -const result2 = replace(stringB, pregunta, respuesta); -console.assert(result2 === stringB.replace(pregunta, respuesta),{ - result: result2, - message: "Test 1 No pasado ", -}); \ No newline at end of file +module.exports = replace; \ No newline at end of file diff --git a/staff/carlos-bock/chain-characters/replace.test.js b/staff/carlos-bock/chain-characters/replace.test.js new file mode 100644 index 00000000..23687d27 --- /dev/null +++ b/staff/carlos-bock/chain-characters/replace.test.js @@ -0,0 +1,23 @@ + + +const ChainCharacters = require("./"); + +const aString = "My favorite color is blue"; +const oldWord = "blue"; +const newWord = "red"; + +const stringB = "Cual es el mejor bootcamp"; +const pregunta = "Cual"; +const respuesta = "ISDI"; + +const result1 = new ChainCharacters(aString).replace(oldWord, newWord); +console.assert(result1.value === aString.replace(oldWord, newWord),{ + result: result1, + message: "Test 1 No pasado ", +}); + +const result2 = new ChainCharacters(stringB).replace(pregunta, respuesta); +console.assert(result2.value === stringB.replace(pregunta, respuesta),{ + result: result2, + message: "Test 1 No pasado ", +}); \ No newline at end of file diff --git a/staff/carlos-bock/chain-characters/slice.js b/staff/carlos-bock/chain-characters/slice.js index ce0cf6e0..acf388a9 100644 --- a/staff/carlos-bock/chain-characters/slice.js +++ b/staff/carlos-bock/chain-characters/slice.js @@ -4,24 +4,15 @@ //si hay dos parametros usa el primer indice hasta llegar al segundo para crear el nuevo string //si los indices sone negativos se cuentan desde el final del string original. -function slice(string, index1, index2) { +const ChainCharacters = require("./constructor"); + +function slice(index1, index2) { let newString = ""; for (let i = index1; i < index2; i++) { - newString += string[i]; + newString += this.value[i]; } - return newString; + return new ChainCharacters(newString); } - - -const string1 = "Madrid es la mejor ciudad del mundo." -const indexOne = 6; -const indexTwo = 12; - - -const result1 = slice(string1, 1, 3 ); -console.assert(result1 === string1.slice(1, 3),{ - result: result1, - message: "Test 1 No pasado ", -}); \ No newline at end of file +module.exports = slice \ No newline at end of file diff --git a/staff/carlos-bock/chain-characters/slice.test.js b/staff/carlos-bock/chain-characters/slice.test.js new file mode 100644 index 00000000..38dfa4de --- /dev/null +++ b/staff/carlos-bock/chain-characters/slice.test.js @@ -0,0 +1,11 @@ + + +const ChainCharacters = require("./"); + +const string1 = "Madrid es la mejor ciudad del mundo." + +const result1 = new ChainCharacters(string1).slice(1, 3 ); +console.assert(result1.value === string1.slice(1, 3),{ + result: result1, + message: "Test 1 No pasado ", +}); \ No newline at end of file