Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. /*
  2. The distance between upper case characters and lower case characters in standard ASCII is 32.
  3. A : 65 a : (65 + 32) = 97
  4. B : 66 b : (66 + 32) = 98
  5. C : 67 c : (67 + 32) = 99
  6. ...
  7. */
  8.  
  9.  
  10. function convertToUpperCase(str) {
  11. var result = ''
  12.  
  13. for (var i = 0; i < str.length; i++) {
  14. var code = str.charCodeAt(i)
  15. if (code > 97) {
  16. result += String.fromCharCode(code - 32)
  17. } else {
  18. result += str.charAt(i)
  19. }
  20. }
  21. return result
  22. }
  23.  
  24. function convertToLowerCase(str) {
  25. var result = ''
  26.  
  27. for (var i = 0; i < str.length; i++) {
  28. var code = str.charCodeAt(i)
  29. if (code > 64 && code < 91) {
  30. result += String.fromCharCode(code + 32)
  31. } else {
  32. result += str.charAt(i)
  33. }
  34. }
  35. return result
  36. }
  37.  
  38. function converter(str, whichCase, invert) {
  39. if (whichCase === "low") {
  40. str = convertToLowerCase(str)
  41. } else {
  42. str = convertToUpperCase(str)
  43. }
  44. //String without first symbol
  45. var strNoFrst = str.substring(1, str.length - 1)
  46. if (invert === true) {
  47. code = str.charCodeAt(0)
  48. if (code > 64 && code < 91) {
  49. result = String.fromCharCode(code + 32)
  50.  
  51. } else {
  52. result = String.fromCharCode(code - 32)
  53. }
  54. str = result + strNoFrst
  55.  
  56. }
  57. return str
  58. }
  59.  
  60.  
  61. console.log (converter('ABdddDDsdfdfFDsdfDdC', 'up', false))
  62. console.log (converter('ABdddDDsdfdfFDsdfDdC', 'low', false))
  63. console.log (converter('ABdddDDsdfdfFDsdfDdC', 'up', true))
  64. console.log (converter('ABdddDDsdfdfFDsdfDdC', 'low', true))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement