Comment On my javascript code

Comment On my javascript code
0.0 0

#1

Hi, Please comment on my following javascript code. Any sort of recommendations, improvement, feedback is most welcomed. Since I have just begun the intermediate section of javascript, I want to make sure my approach is right. Below are the working solutions of FCC problem “seek and destroy” and “spinal tap case”.

Seek and Destroy
link : https://repl.it/@ridafatima15h1/Seek-and-Destroy

function destroyer(arr) {
  let test = 0;
  let indexArr = [];
  let j = 0;
  let k = 1 ;

  while(k < arguments.length){
    while(test != -1){
      test = arr.indexOf(arguments[k], test);
      //console.log("test: ", test);
      if(test != -1){
        indexArr.push(test);
        test = test + 1;
      }
    }
    //console.log(indexArr);
    while(j < indexArr.length){
      delete arr[indexArr[j]]; //leaves back nulls
      j++;
    }
    k++;
    j = 0;
    indexArr = [];
    test = 0;
  }
  arr = arr.filter(Boolean); //filters nulls
  console.log("filtered array: ", arr);
  return arr;
}

//Tests
destroyer([2, 2, 3, 1, 2, 3, 2], 2, 3);

Spinal Tap Case
link: https://repl.it/@ridafatima15h1/Spinal-Tap-Case

//Function
function spinalCase(str) {

	console.log("Input string:", str);

  //**Declarations and Initioalizations**/

	//Regular Expressions
	let regexSpace = /(\s+)/g;
	let regexUnderScore = /(\_+)/g;
	let regexDash = /(\-+)/g;
	let regexCapital = /[A-Z]/g;

	//Algorithm Variables
	let strLength = str.length;
	let capitalCharIndex = 0;
	let capitalChar = "";
	let tempString_0 = "";
	let tempString_1 = "";

	//**Algorithm**/
	while(capitalCharIndex != -1){
		capitalCharIndex = str.search(regexCapital);
		if(capitalCharIndex === -1){
			continue;
		}
		//console.log("capitalCharIndex: ", capitalCharIndex);//Debugging
		if(capitalCharIndex === 0){
			capitalChar = str.charAt(capitalCharIndex);capitalChar = capitalChar.toLowerCase();
			tempString_0 = str.substring(0, capitalCharIndex+1);
			tempString_1 = str.substring(capitalCharIndex+1, strLength);
			str = capitalChar + tempString_1;
			//console.log("print:",str); //Debugging
		}
		if(capitalCharIndex != 0){
			capitalChar = str.charAt(capitalCharIndex);capitalChar = capitalChar.toLowerCase();
			tempString_0 = str.substring(0, capitalCharIndex);
			tempString_1 = str.substring(capitalCharIndex+1, strLength);
			str = tempString_0 + "-" + capitalChar + tempString_1;
			strLength = str.length;

			//Debugging
			//console.log("capitalChar: ", capitalChar); 
			//console.log("tempString_0: ", tempString_0);
			//console.log("tempString_1: ", tempString_1);
			//console.log("str: ", str);
		}
	}
	str = str.replace(regexUnderScore, "");
	str = str.replace(regexDash, " ");
	str = str.replace(regexSpace, "-");

	//**Debugging**
	console.log("spinalCase string:", str);
  return str;
}

//Tests
spinalCase('This Is Spinal Tap');
console.log("============================");
spinalCase("The_Andy___Griffith_Show");
console.log("============================");
spinalCase("Teletubbies say Eh-oh");
console.log("============================");
spinalCase("AllThe-small Things");
console.log("============================");
spinalCase("thisIsSpinalTap");

Thank You All In Advance.
regards Rida.


#2

To avoid spoiling a full working solution for other campers who may not yet want to see a complete solution, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Please do this for all the other posts you recently created too.

If you are passing the tests, you are fine for now. If you have anything specific you are questioning, make sure to be specific in your question instead of just asking for “recommendations, improvement, feedback”.


#3

Hi, @randelldawson I have added the “spoiler” tags as asked by you, my apologies I was neither aware of this FCC rule nor about the spoiler tags, so thank you for bringing it to my attention.

And, about the question, being a beginner in javascript I wanted a general review of my code just for the sake of improvement. Nevertheless, I will make sure to be specific about my questions in the future.


#4

It is not a hard fast rule, but what tends to happen with these types of threads, is everybody just starts posting their solutions, because nothing specific is being asked. And as you probably know, everybody thinks “their” solution is better. :slight_smile:


#5

@randelldawson yes are right about this, I totally understand your point :+1::slightly_smiling_face:


#6

You can take a look at the FCC Guide entries for most of the challenges and compare your solution to those in the guide.

Seek and Destroy

Spinal Tap Case


#7

@randelldawson Thank you for this Link! I did not know about this guide actually :sweat_smile: . I think that’s the help I actually needed. It will be helpful in further optimizing my code.:+1:


#8

Here is my spinal tap --> https://repl.it/@John_Nicole/Intermediate-Algorithm-scripting-Spinal-Tap-Case

function spinalCase(str) {
    str = str.split('');
    for (var i = 0; i < str.length; i++) {
        if(str[i] == "_") {
            str[i] = ""
        }
        if (/^[A-Z]/.test(str[i + 1]) === true && str[i] !== " ") {
            // is it not a space but the next character is captilized?
            str[i] = str[i] + '-';
        }
        if (str[i] == ' ') {
            str[i] = '-';
        }
    }
    return str.join('').toLowerCase();
}
spinalCase('TheAndyGriffith Show');

Explanation:

People SeprateWordsLikeThis using a capitalized letter to signify the end of one word and the start of another.

So as you can see where my comment is on my code, i test the next letter and add a “-” to the current item if it is capital.

This does not allow you to keep words together that are all caps.


#9

#10

#11

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.