Algorithm challenges code review. BEWARE: Spoilers )

Algorithm challenges code review. BEWARE: Spoilers )
0.0 0

#1

Hi everyone,

I just wanted to thank first all the creators of freeCodeCamp and all the great contributors to this forum (you guys), I usually read freeCodeCamp posts, but it’s my very first (might be second, or third? I don’t remember posting though :)) post here, I am not even sure, whether I am doing this right.
Folks, do you know some great place where I can ask to peer review my code from experienced developers?
I have decided to post here all my solutions to the Basic Algorithm Scripting. I really want your feedback. If you see the problem in my code, can you please let me know?
Please do not read any further if you don’t want to get spoilers.

#1.

function reverseString(str) {
  var myAr =  str.split("");
  newString = myAr.reverse().toString().replace(/,/g,"");
  return newString;
}

reverseString("hello");

#3

function palindrome(str) {
  var newStr = str.replace(/[\W_]/g, '').toLowerCase();  
  var result = "";
  for (var i = newStr.length - 1; i >= 0; i--) {
    result += newStr.charAt(i);
  }
  return result === newStr;
}

#4


function findLongestWord(str) {
  var strSplit = str.split(" ");
  var longest = "";
   for (var i=0;i<strSplit.length;i++){
     if (strSplit[i].length > longest) {
        longest = strSplit[i].length;
     }
    }
  return longest;
}

#2

some feedback from stackoverflow: code won’t work with strings that contain a , character,
Need to rewrite to make it work.


#3

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Have you seen Code Review Stack Exchange?

You can also blur your code so they’re not immediately visible by using spoiler tags.

[spoiler]

...code...

[/spoiler]

#4

That’s awesome! Thank you for the info!
1.
I have redone that exercise, so now it works with the comma character.


function reverseString(str) {
  var myAr =  str.split("");
  newString = myAr.reverse().join("");
  return newString;
}

reverseString("mama, myla");



#5

While this code will work, best practice is to protect your internal properties from direct/global access.

“newString” is declared without the “var” keyword, making it a global variable. That means it can be seen and access from code anywhere. You don’t want that. You would want to control how newString is handled, so by declaring it with a "var newString … ", you restrict the scope to local use within the reverseString() function only.

You’ve got a great attitude in asking for code review to improve your skills.
Are you up for another challenge?

You used the built-in array function “reverse()” to reverse the order of the array elements. Built-in functions are a great convenience to speed up programming and ensure consistent results. But since the current lesson you’re on is about algorithms, try completing this exercise without using the reverse() function.


#6

rfdev1000, thanks for the input.

I found some ways how to do it without reverse here:

function reverse(str) {
var result = “”;
for (var i = str.length - 1; i >= 0; i–) {
result += str.charAt(i);
}
return result;
}

I checked the second challenge and I knew by myself, that it can be solved by recursive functions and I found elegant solution here (https://medium.freecodecamp.com/how-to-factorialize-a-number-in-javascript-9263c89a4b38), however, I am not that comfortable with the notion yet. So I will read more on the topic and I will continue after I get more info.

Thanks for support,


#7

Do you know is it OK, if I edit my question and I post more answers there, so that it will be all together on top?