Pig Latin Translator - JavaScript

后端 未结 10 1782
别跟我提以往
别跟我提以往 2020-12-07 04:09

So for my cit class I have to write a pig Latin converter program and I\'m really confused on how to use arrays and strings together. The rules for the conversion are simpl

10条回答
  •  -上瘾入骨i
    2020-12-07 05:13

    Your friends are the string function .split, and the array functions .join and .slice and .concat.

    • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array#Accessor_methods
    • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#Methods_unrelated_to_HTML

    warning: Below is a complete solution you can refer to once you have finished or spent too much time.

    function letters(word) {
        return word.split('')
    }
    
    function pigLatinizeWord(word) {
        var chars = letters(word);
        return chars.slice(1).join('') + chars[0] + 'ay';
    }
    
    function pigLatinizeSentence(sentence) {
        return sentence.replace(/\w+/g, pigLatinizeWord)
    }
    

    Demo:

    > pigLatinizeSentence('This, is a test!')
    "hisTay, siay aay esttay!"
    

提交回复
热议问题