suffixcipher

 avatar
unknown
javascript
3 years ago
738 B
5
Indexable
function suffixCipher(sentence, obj) {
  let words = sentence.split(" ");
  let newStr = [];
  for(let i=0; i<words.length; i++){ //iterate each word
    let currentWord = words[i];
    let add_original_boolean= true; //default add the word
    for(let key in obj){ //loop through keys (2, ly and ize) and see if word contains any of the suffix
      let cb = obj[key];
      if(currentWord.endsWith(key)){ //if word ends in suffix
        add_original_boolean = false; //make sure to not add repeat
        newStr.push(cb(currentWord));
      }
    }
    if(add_original_boolean){ //return original word if at the end, word has none of those suffix
      newStr.push(currentWord);
    }
  }
  return newStr.join(" ");
}
Editor is loading...