## DEV Community is a community of 700,827 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Algorithm Scripting: Notes & Problems

Randy Rivera
Once I truly put my mind on something, I won't ever stop.
• 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));
``````
``````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');
``````
``````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");
``````

``````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");
``````

``````function myReplace(str, before, after) {