KATA EXERCISE JS switch语句和回调函数

时间:2018-04-06 14:35:02

标签: javascript

我正在尝试解决此练习,其中所有输出必须显示在终端上。

任务

您仍然会获得要转换的输入字符串。但是,这一次,您还可以使用套管样式。以下代码块将描述要支持的所有外壳样式。您可能还会收到一系列套管样式,并且应该应用每种样式。

每种套管样式的优先顺序如下,应首先处理列表中较高的值:

骆驼,帕斯卡,蛇,烤肉串,头衔 元音辅音 上,下

测试和输出应如下:

makeCase("this is a string", "camel") === "thisIsAString";

makeCase("this is a string", "pascal") === "ThisIsAString";
makeCase("this is a string", "snake") === "this_is_a_string";
makeCase("this is a string", "kebab") === "this-is-a-string";
makeCase("this is a string", "title") === "This Is A String";
makeCase("this is a string", "vowel") === "thIs Is A strIng";
makeCase("this is a string", "consonant") === "THiS iS a STRiNG";
makeCase("this is a string", ["upper", "snake"]) ==="THIS_IS_A_STRING";

这是我到目前为止提出的代码。道歉很长。我正在学习javascript并且是一个菜鸟。

      var makeCase = function(input, font)
      {
      var array = input.split(' ')

      var newarray = []
      var rez = []
      var tier = []
      font = font.toString()

      // **************************************CALLBACK FUNCTIONS FONT ********************************

            var camel = function()
            {
              for(var x = 0; x < array.length; x++)
                    {
                      newarray.push(array[x].charAt(0).toUpperCase()+array[x].slice(1));
                    }

                    var b = newarray.join('');

                    var pieces = b.split("");

                    var lower = pieces[0].toLowerCase()

                    var result = pieces.splice(0,1,lower)

                    rez.push(pieces.join(""))
            }


              var pascal = function()
              {
                for(var x = 0; x < array.length; x++)
                    {
                      newarray.push(array[x].charAt(0).toUpperCase()+array[x].slice(1));
                    }
                    var b = newarray.join('');

                    var pieces = b.split("");

                    var upper = pieces[0].toUpperCase()

                    var result = pieces.splice(0,1,upper)

                    rez.push(pieces.join(""))
              }

              var snake = function()
              {
                var array = input.split("")
                  for(var x = 0; x < array.length; x++)
                        {
                          newarray.push(array[x]);
                          if (array[x] === ' ')
                          {
                            newarray.push("_")
                          }
                        }
                          var pieces = newarray.join(" ").replace(/\s/g,'')
                          rez.push(pieces)
              }

              var kebab = function()
              {
                var array = input.split("")
                  for(var x = 0; x < array.length; x++)
                        {
                          newarray.push(array[x]);
                          if (array[x] === ' ')
                          {
                            newarray.push("-")
                          }
                        }
                          var pieces = newarray.join(" ").replace(/\s/g,'')
                          rez.push(pieces.join(""));
              }

             var title = function()
             {
                for(var x = 0; x < array.length; x++)
                    {
                      newarray.push(array[x].charAt(0).toUpperCase()+array[x].slice(1));
                    }
                    var b = newarray.join(' ');

                    var pieces = b.split("");

                    var upper = pieces[0].toUpperCase()

                    var result = pieces.splice(0,1,upper).join("")

                    rez.push(pieces.join(""))
             }


             var vowel = function()
             {
              var array = input.split("")
                for(var x = 0; x < array.length; x++)
                    {
                      if (array[x] === 'a' || array[x] === 'e' || array[x] === 'i' || array[x] === 'o' || array[x] === 'u')
                      {
                        newarray.push(array[x].charAt(0).toUpperCase(array[x]))
                      }
                      else
                      {
                        newarray.push(array[x])
                      }
                    }
                    var pieces = newarray.join(' ');
                    tier.push(pieces.join(""))
             }

              var consonant = function()
             {
              var array = input.split("")
                for(var x = 0; x < array.length; x++)
                    {
                      if (array[x] === 'a' || array[x] === 'e' || array[x] === 'i' || array[x] === 'o' || array[x] === 'u')
                      {
                        newarray.push(array[x].charAt(0).toLowerCase(array[x]))
                      }
                      else
                      {
                        newarray.push(array[x].charAt(0).toUpperCase(array[x]))
                      }
                    }
                    var pieces = newarray.join(' ');
                    tier.push(pieces.join(""))
             }


      if (font.constructor !== Array)
      {
        switch (font)
          {
            case 'camel':
            camel()
            return rez.join('')
            break

            case 'pascal':
            pascal()
            return rez.join('')
            break

            case 'snake':
            snake()
            return rez.join('')
            break

            case 'kebab':
            kebab()
            return rez.join('')
            break

            case 'title':
            title()
            return rez.join()
            break

            case 'vowel':
            vowel()
            return tier.join("")
            break

            case 'consonant':
            consonant()
            return tier.join("")
            break
          }
      }

      // **************************************TIER 2 AND 3 FONTS ********************************

      else if (font.constructor === Array)
      {
        switch (font)
        {
          // How do I join cases togather and combine my functions when multiple styles are needed?
        }
      }


      }

基本上我需要弄清楚当要求2种样式/字体时如何应用正确的字体。提前致谢

1 个答案:

答案 0 :(得分:0)

在同一输入上调用多个功能

var functions = {
    pascal: (input) ->
       ...
    uppercase: (input) ->
       ...
}

if (typeof font !== 'array') {
    font = [font]
}

$.each(
    font,
    (fontType) ->
        input = functions[fontType]();
);

旁注

所有函数基本上都是一样的,所以字体应该只定义是否需要保持字母大小写和分隔符(思维边缘情况,如camelCase)