Guest User

Untitled

a guest
Dec 16th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. //AlgoChal: p107, Reverse Word Order
  2. function reverseStr(str, p_bool=false){
  3.  
  4. var arr = strToArr(str, p_bool);
  5. var newstr= "";
  6.  
  7. for(var i = arr.length-1; i>=0; i--){
  8.  
  9. if (i==0){
  10. newstr+=arr[i];
  11. }
  12.  
  13. else{
  14. newstr+=arr[i];
  15. newstr+=" ";
  16. }
  17. }
  18. return newstr;
  19. }
  20.  
  21.  
  22. //AlgoChal: p107, String to Word Array
  23. function strToArr(str, punct=false){
  24. // console.log("I am in strToArr and punct is: ", punct);
  25. var arr= [];
  26. var temp = "";
  27.  
  28. for(var i = 0; i<str.length; i++){
  29. if(punct){
  30. if (str[i]>='A' && str[i]<='z'){
  31. temp+=str[i];
  32. }
  33. else if(str[i]==" " && i!=0){
  34. arr.push(temp);
  35. temp="";
  36. }
  37. else{
  38. continue;
  39. }
  40. }
  41.  
  42. else{
  43. if(str[i]==" " && i!=0){
  44. arr.push(temp);
  45. temp="";
  46. }
  47. else{
  48. temp+=str[i];
  49. }
  50. }
  51. }
  52.  
  53. if(temp.length>0){
  54. arr.push(temp);
  55. }
  56. return arr;
  57. }
  58.  
  59. //reverseStr('I -am me', true);
  60.  
  61. //AlgoChal: p107, Unique Words
  62. function uniqueStr(str){
  63.  
  64. var arr = strToArr(str);
  65. var obj = {};
  66. var count = 0;
  67.  
  68.  
  69. console.log(arr.length);
  70. for(var i = 0; i <= arr.length-1; i++){
  71. console.log("This is step: ",i);
  72. console.log(obj);
  73. console.log(Object.values(obj));
  74. console.log(arr[i]);
  75. //if (arr[i] in obj){
  76. if(Object.values(obj).includes(arr[i])){
  77. console.log("Evaluated to true: ", i);
  78. continue;
  79. }
  80.  
  81. else {
  82. obj[count] = arr[i];
  83. console.log("printing value at count after inserting: ",obj[count], " and count ", count);
  84. count++;
  85. }
  86. }
  87.  
  88. var newstr = "";
  89.  
  90. for(i=0; i<count; i++){
  91. newstr+=obj[i];
  92. newstr+=" ";
  93. }
  94. return newstr;
  95. }
  96.  
  97. uniqueStr("I am me or not me");
Add Comment
Please, Sign In to add comment