Trouble With Algorithms

Trouble With Algorithms
0

#1

Hey folks!

I’m sure I’m not the only one here having trouble with learning algorithm functions. But, that whole section didn’t really seem to explain how algorithms work very clearly. I keep going back to different sections to wrap my head around things, but it still doesn’t make sense. Does anyone have any suggestions as to what I should do to try to grasp this concept better? I checked out the CodeAcademy “Recursion in JavaScript” section, and that helped me out somewhat. Right now I’m just really stuck on the “Check for Palindrome” challenge.

Should I go back through the section and re-do some of the challenges? I’m so stuck with this one, I’m not even sure where to begin, and I kind of wish the section elaborated more on using algorithms and recursion. Things like why we use them, the proper application for nested loops, while loops, if/else vs. switch/case. I think that for such an intense concept, the information was fairly slim.

Help a camper out? (I’m not looking for answers, I’m just looking for a step in the right direction for better understand this) Thank you all!


#2

You can think of an algorithm as a series of steps to complete a task. Think about a recipe for your favorite meal. A recipe is an algorithm. While you are figuring out the algorithm, it sometimes helps to write down the general steps you will need to take to accomplish the task. These written steps can include “pseudo-code” which might describe a looping scheme or a “return” statement.

For the palindrome challenge, I would first look at all the tests at the bottom and make sure you first understand why each test result is correct. If you don’t understand that, then you need to review the instructions and research the definition of what a palindrome is.

Once you understand why the test results are correct, then start thinking about how you might take a string value and iterate through it or manipulate it in some way to solve the challenge. There are multiple ways to approach this problem. Try to write out some pseudo-code of your steps before attempting to write full code. When you get your pseduo-code written, use one of the test cases and walk through your algorithm to see if your would arrive at the correct result using your logic.

Once you are convinced your written algorithm should work, then start thinking about the various JavaScript features and syntax you have already learned to piece together some code for your algorithm. If you are having trouble finding a specific function or can not remember how to declare a for loop, the use google to help you find documentation or articles that explain how to use them.

Once you have some code and are either not understanding why your code does not work or why you are getting specific errors in the console, then reply back to this post with any code written and be as detailed as possible in your questions. The more you explain about what trouble you are experiencing, the greater number of people will respond back with helpful answers for you.


#3

here’s what I’d do

0 . relax - grab a coffee and snack - keep munching for the next 8 steps
1 . simply read the problem statement and instructions once - no need to completely understand anything
2 . work through any provided examples in your head once
3 . look at the test cases in order
4 . carefully examine the inputs of a test case - how many inputs are there - what is the type of each input - what is the meaning of each input
5 . carefully examine the output and result of a test case - what is the type of the output - what does the output mean?
6 . Make sense of the relationship between the output and the inputs - does it jibe with your understanding of the problem? read and re-read the problem statement till it does
7 . do not solve any test case or the problem in general - the goal is to simply validate the test case against the problem statement
8 . repeat for 2-3 test cases

after completing the steps above you’re ready to approach solutions to the problem

now to work

put the snack down - seriously
grab paper and pencil
we want to approach a solution - not necessarily actually solve the problem since with programming there’s no actual solution till the code works
we just want a preliminary phase - an intermediate stage that will guide us and to which we can return and reconsider when the code turns ornery

write the first test case - use whatever notation you like
e.g. the first palindromes test case is palindrome(“eye”) should return a boolean

ok - that’s stupid
write the second test case - palindrome(“eye”) should return true - this is what I’d write

eye -> true

the idea now is to describe a solution in everyday language - no code - no variables - no arrays - no loops - no lambda combinators
pretend you’ll need to explain the solution to a fifth-grader - from 100 years ago - before fifth-graders used to write facebook apps

while we want to avoid programming terms at this stage we do want to be as precise and most importantly as deterministic as possible
we want a solution with instructions that are repeatable no matter who or what follows them
it’s just that our early instructions will be in ordinary english given to the hypothetical fifth-grader


Thinking like a programer
Title case a sentence without split
#4

I’m sure I’m not the only one here having trouble with learning algorithm functions.

Oh, good god, no!

Some people “get” algorithms pretty easily, and some have to work hard at it. And there’s a lot of gray areas in the middle. But maybe that just means that some other aspect of web design/development will be easier for you. We all come to this with different strengths and weaknesses.

But algorithms are pretty important. It’s just a different way of thinking. How do you sold a problem with a limited number of commands. You have commands to assign variables, manipulate data, control program flow, (like for loops, if/then/else, etc.)

FCC is not meant to be a complete course. It is expected that you use some outside resources for areas where you need extra work. I might suggest checking out youtube videos. When I search for “algorithms for javascript” a lot of videos pop up. Search through them, work your way through some problems with them,

But ultimately don’t get discouraged. And don’t be afraid to ask the forum if you get stuck on one or even if your not sure you understand something you “solved”.


#5

Thank you all for your kind and very helpful advice. I managed to get through the Title Case A Sentence challenge. It took me a couple hours of frustration, confusion, and maybe a few tears, but I got through it. And my code is concise. Thank you all again for the help! <3

function titleCase(str) {
  var newStr = str.toLowerCase().split(' ');
  for (var i = 0; i < newStr.length; i++){
    newStr[i] = newStr[i].replace(newStr[i][0],  newStr[i][0].toUpperCase());
    
  }
  newStr=newStr.join(' ');
  return newStr;
}

titleCase("mOcKiNG sPonGEbOb mEmE");

#6

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#7

Thank you kindly! I will keep that in mind for future posts. :sunglasses:


#8

that looks pretty good - you took the ball and ran with it


#9

Good job. Just keep working that algorithm muscle and it will get stronger.


#11

I remember looking through some old forum posts where p1xt recommended one to take a detour and go through ydkjs and cs50. By the time I went to cs50, I was more than halfway done with the intermediate algorithms but I agree with her advice. Freecodecamp doesn’t really teach you much more than syntax for JavaScript. Going through cs50 for a bit will help you to develop the problem solving skills for the algorithms and projects on freecodecamp. Personally, for javascript I would recommend something like Understanding the Weird Parts first since it’s easier to understand them going through ydkjs after.