Algorithm Scripting: Notes & Problems
Tags: javascript, algorithms, beginners, devops
Published: 2021-07-26 22:18:14

* 4:00 on a Saturday. Continuing to learn and study more Algorithms Scripting. Today we'll go over a few and then continue this day by day. This is one of many posts, stories and problems that I myself will learn and overcome. Bare with me because I'm still fairly new at this.
One of the problems today I was provided with an initial array followed by one or more arguments. I had to remove all elements from the initial array that are of the same value as these arguments.
* Note: I have to use the arguments object.
```
function remove(arr) {

}

console.log(remove([1, 2, 3, 1, 2, 3], 2, 3));
```
* Answer:
```
function remove(arr) {
  let args = [...arguments];
  args.splice(0, 1)

  return arr.filter(function(num) {
    if (args.indexOf(num) === -1 ) {
      return num;
    }
  })
}

(remove([1, 2, 3, 1, 2, 3], 2, 3)); //  when console.log it will display [1, 1];
```

* Continuing Forward I will always have everything that I've done so far in one day on a single post. Sometimes it various because currently I am busy with a lot of stuff. So some days I get a lot of work in and others not so much.
* You can definitely go and check out my other blog and articles here.
* Next problem wanted me to convert a string to all-lowercase-words-joined-by-dashes.
* Problem
```
function fix(str) {
  return str;
}

fix('Check Out My Rank');
```
* Answer:
```
function fix(str) {
  let regexStr = str.replace(/([a-z])([A-Z])/g, "$1 $2");
  let spacesOr = regexStr.replace(/\s|_/g, "-");
  return spacesOr.toLowerCase();
}

console.log(fix('Check Out My Rank')); will display check-out-my-rank
```
#### Next question I encountered.
* The following task was quite challenging, mainly because I didn't really know about Pig Latin per se. Anyways Pig Latin basically is a way of altering English Words. The rules are for this task are as followed..
- If a word begins with a consonant(basically if it isn't a vowel), take the first consonant or consonant cluster, move it to the end of the word , and add ay to it.
-If a word begins with a vowel, just add way at the end.
Now with the provided info I have been given lets translate the string to Pig Latin.
Problem:
```
function changeIntoPigLatin(str) {
  return str;
}

changeIntoPigLatin("algorithm");
```
Answer:
```
function changeIntoPigLatin(str) {
  let vowel = str.match(/[aeiou]/); // we don't use g (global)
  let firstPosition = str.indexOf(vowel);

  if (firstPosition > 0) {
    return str.slice(firstPosition) + str.slice(0, firstPosition) + "ay";
  } else if (str.indexOf(vowel) === -1) {
    return str + "ay"
  }
  return str + "way";

};

 console.log(changeIntoPigLatin("algorithm")); // will display algorithmway
```
* Now, we have a situation where we should perform a search and replace on the sentence using the arguments provided and return the new sentence.
* Looking at our code the first argument is the sentence to perform the search and replace on and the second argument is the world that you will replace. Finally the third argument is what you will be replacing the second argument with.
For this particular problem we must save the case of the first character in the original word when we are replacing it. An example would be if you need to replace the word `Games` with the word `movies`, it should be replaced as `Movies`.
Problem:
```
function myReplace(str, before, after) {
  return str;
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
```
Answer:
```
function myReplace(str, before, after) {
if (before[0] === before[0].toUpperCase()) {
after = after[0].toUpperCase() + after.slice(1);
} else if (before[0] === before[0].toLowerCase()) {
after = after[0].toLowerCase() + after.slice(1);
}
return str.replace(before , after)
}
console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting")); // will display He is Sitting on the couch
```