Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AlgoChal: p107, Reverse Word Order
- function reverseStr(str, p_bool=false){
- var arr = strToArr(str, p_bool);
- var newstr= "";
- for(var i = arr.length-1; i>=0; i--){
- if (i==0){
- newstr+=arr[i];
- }
- else{
- newstr+=arr[i];
- newstr+=" ";
- }
- }
- return newstr;
- }
- //AlgoChal: p107, String to Word Array
- function strToArr(str, punct=false){
- // console.log("I am in strToArr and punct is: ", punct);
- var arr= [];
- var temp = "";
- for(var i = 0; i<str.length; i++){
- if(punct){
- if (str[i]>='A' && str[i]<='z'){
- temp+=str[i];
- }
- else if(str[i]==" " && i!=0){
- arr.push(temp);
- temp="";
- }
- else{
- continue;
- }
- }
- else{
- if(str[i]==" " && i!=0){
- arr.push(temp);
- temp="";
- }
- else{
- temp+=str[i];
- }
- }
- }
- if(temp.length>0){
- arr.push(temp);
- }
- return arr;
- }
- //reverseStr('I -am me', true);
- //AlgoChal: p107, Unique Words
- function uniqueStr(str){
- var arr = strToArr(str);
- var obj = {};
- var count = 0;
- console.log(arr.length);
- for(var i = 0; i <= arr.length-1; i++){
- console.log("This is step: ",i);
- console.log(obj);
- console.log(Object.values(obj));
- console.log(arr[i]);
- //if (arr[i] in obj){
- if(Object.values(obj).includes(arr[i])){
- console.log("Evaluated to true: ", i);
- continue;
- }
- else {
- obj[count] = arr[i];
- console.log("printing value at count after inserting: ",obj[count], " and count ", count);
- count++;
- }
- }
- var newstr = "";
- for(i=0; i<count; i++){
- newstr+=obj[i];
- newstr+=" ";
- }
- return newstr;
- }
- uniqueStr("I am me or not me");
Add Comment
Please, Sign In to add comment