coderbyte_Letters Changes

写在前边

之所以想把自己做codebyte的经历写下来:
一是,为了养成写博客的习惯;
二是,学着使用markdown这种轻量级的标记语言;
三是,学习javascript语言

coderbyte link

https://coderbyte.com/
2017年最受欢迎的10个编程挑战网站

题目信息

  • Challenge/描述
    Using the JavaScript language, have the functionLetterChanges(str)take thestrparameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie.c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.
  • Sample Test Cases/样例
Input:"hello*3"
Output:"Ifmmp*3"

Input:"fun times!"
Output:"gvO Ujnft!"

题目分析

阅读题目,总结代码实现时注意点:

  • 1、字符串获取字符数组
    str.split("")可以将字符串转换成字符数组
  • 2、字符与ascii值得互相转换
    ‘a’.charCodeAt()String.fromCharCode(65)
  • 3、输入英文字符的大小写
  • 4、元音英文字符的处理
    “a e i o u”
  • 5、非元音英文字符的处理
  • 6、非英文字符的特殊处理
  • 7、z/Z英文字符的特殊处理

自己的代码

function LetterChanges(str) {
// code goes here
var origin_str = str.split("")
for(var i = 0; i < origin_str.length; i++)
{
  var begin_a = 'a'.charCodeAt();
  var end_z = 'z'.charCodeAt();
  var voewl = new Array();
  voewl[0] = 'a'.charCodeAt();
  voewl[1] = 'e'.charCodeAt();
  voewl[2] = 'i'.charCodeAt();
  voewl[3] = 'o'.charCodeAt();
  voewl[4] = 'u'.charCodeAt();
  var voewl_case = new Array();
  var begin_case_a = 'A'.charCodeAt();
  var end_case_z = 'Z'.charCodeAt();
  voewl_case[0] = 'A'.charCodeAt();
  voewl_case[1] = 'E'.charCodeAt();
  voewl_case[2] = 'I'.charCodeAt();
  voewl_case[3] = 'O'.charCodeAt();
  voewl_case[4] = 'U'.charCodeAt();
  temp_ascii = origin_str[i].charCodeAt();
  if(temp_ascii >= begin_a && temp_ascii < end_z)
  {
    temp_ascii = temp_ascii + 1;
    if(temp_ascii== voewl[0] || temp_ascii == voewl[1] || temp_ascii == voewl[2] || temp_ascii == voewl[3] || temp_ascii == voewl[4])
    {
      temp_ascii = temp_ascii - voewl[0] + voewl_case[0];
    }
    else
    {
    temp_ascii = temp_ascii;
    }
  }
  else if(temp_ascii >= begin_case_a && temp_ascii < end_case_z)
  {
    temp_ascii = temp_ascii + 1;
  }
  else if(temp_ascii == end_z)
  {
    temp_ascii = begin_a;
  }
  else if(temp_ascii == end_case_z)
  {
    temp_ascii = begin_case_a;
  }
  else
  {
    temp_ascii = temp_ascii;
  }
  origin_str[i] = String.fromCharCode(temp_ascii);
  }
  return origin_str.join("");
}
// keep this function call here
LetterChanges(readline());

别人的代码

function LetterChanges(str) {
  function incrementLetters(origLetter){
  return String.fromCharCode(origLetter.charCodeAt(0)+1);
}
function capVowels(lowVowel){
  return lowVowel.toUpperCase();
}
  str = str.toLowerCase().replace(/[a-y]/g, incrementLetters).replace(/z/g, 'a').replace(/[aeiou]/g, capVowels);
  return str;
}
// keep this function call here
LetterChanges(readline());

你可能感兴趣的:(coderbyte_Letters Changes)