suffixcipher

mail@pastecode.io avatar
unknown
javascript
2 years ago
738 B
2
Indexable
Never
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(" ");
}