I need help. (Basic Algorithm Scripting, etc.)

I understand I should be taking about 2-3 hours on every challenge in Basic Algorithm Scripting, however it seems as if I can not obtain the solution unless I look up hints from the FCC wiki and other Forum posts regarding the same question (along with Google). I’m hitting the point where I can not go further after hitting certain points, and while I have started reading YDKJS (recommended by @P1xt and others) and gotten up to the second book in the series, at the end of the day, I can not come up with the complete solution for the challenges by myself. Am I doing the right thing where I am referencing others and other sources to get the answer, or should I take a step back into CodeAcademy or similar (I tried CodeWars and couldn’t even grasp that)? Basically, should I keep doing what I’m doing, and if not, where do I go/what do I do?

2 Likes

What you’re doing is fine, just after you have finished it, go back over your code and make sure you understand everything and how it solves the problem. A lot of the time the tips and suggestions you will find involve using advanced methods that can make it harder for newcomers to understand. Ask yourself simple questions like “why did they do it this way?” or “how else could I have done it?” Then try to rewrite the code using a different method. (This can seem boring and pointless but it does help). The biggest thing is to breakdown the problem into small pieces and then solve each little one until you are done.

I think after some thought, I know what to logically do especially after reading the wiki hints, but I freeze up at thinking up the solution with JS itself. For Example:

Find the Longest Word in a String

function findLongestWord(str) {
  
  var array = str.split(" ");
  
  
  for (var i = 0; i < str.length; i++){
    
  }
  
  return;
}

findLongestWord("The quick brown fox jumped over the lazy dog");

Using split was obvious because I need to seperate the string in order to break it down into smaller strings, which are essentialy the words, by spaces.
I believe split stores them in some sort of array and that I may need to use a global variable in order to keep track of what that largest string is in that array, but at this point I’m not sure how to do that with an if statement and so on and so forth, I know I should return the largest string once that’s determined through the global variable but that’s it. This is my major problem, I know what to do just not how to do it with the code itself.

Try sorting the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The compare function for the sort can be a comparison of the string length.
If you sort smallest to largest, pop the last value. Do the opposite, shift the value off the front of the array.

you need to debug code so you can see what is going on … simple console.log() will help a lot eg var array = str.split(" "); … console.log(array) … this will show you what is in the array … i would recommend using repl.it as it consoles to the screen making it very handy … use pythontutor.com … as using this you can step through the code line by line … and learn to use devtools from your browser eg i use chrome so i use chrome dev tools to step through the code to see what is happening.
I believe joint number one most important thing when learning to code is learning to debug and step through your code … as its hard to write code if you dont know for sure what is happening.
other most important things is breaking down a problem and working out what needs to be done before you try and code it eg working out the logic and writing it in sudo code … this part you kind of got a grasp on but you are not working it through … eg split string … store it in an array create variable to store length of largest word … (not a global variable just a variable … never create a global variable unless its absolutely necessary) eg var result =0; … next need to loop through array to check length of words … so a for loop is fine here … then if word length is bigger than result make result= to length of word … then return result

If you can write it out in sudo code … then you can write the code … and you will find it a lot easier … if you get errors … debugging and stepping through your code will show you where you are going wrong … but if you are just writing code without fully understanding the problem and not debugging your code you are going to struggle big time.

1 Like

I struggle quite a bit with figuring things out in the advanced algorithms (logically). At first I struggled with the beginner and intermediate but after taking a break and codewarring for a bit (2-3 weeks 1h/day) it improved not only my logical thinking but I also discovered new JS methods. Do you have any advice on how someone could improve their “logical thinking”?

Thank you! CS50 was already on the agenda, and I will also add Effective Thinking through Mathematics.

What would you recommend to someone who can think things logically but can not think of the required Javascript to make that logic work?

If you have the logic and are just missing code knowledge I would say you need to learn how to read the documentation.
20 years ago our Java teacher asked us to write the shortest piece of code to perform some task. We were all trying to optimize for loops for a solid 10min but could not get it done in less than 6 lines.
He then showed his solution: a one-liner.
All we had to do, and should have done, was to look at the String documentation and find the one method that was required for the job.
Lesson learnt! Whenever a learn a new library or language, I dive into the doc!

If that’s what you meant by required JavaScript then that’s what you need to do: understand how to find info in the doc.

I occasionally go to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference just to see if there isn’t a method that would make my code simpler/shorter/less prone to errors.

Hope this helps.

3 Likes

Take time to think about it
Take a sheet of paper, a pencil
and think :
how can I obtain the good result without any langage else but plain english

the rest is just a matter of vocabulary

I love how you worded that P1xt! I have some faith in my coding abilities because I feel I am fairly decent at breaking apart a problem and coming up with an “explanation of what needs to happen in English” as you put it, but as a newbie I struggle with the JavaScript part. Struggle in a good way - I love trying to figure it out by learning.

repl.it is all I use since I discovered it last month. It’s great!

Thank you all for every piece of advice, I will use it for the future on my quest to learn JS!

Planning out your solution on paper before you start coding helps a lot.

Here is my advice for what it is worth. Whenever I am tackling a complex coding problem my steps are as follows:
1.Brainstorm and list the basic steps you need to get to the solution. If it is a very large problem you can refine it by splitting into sub-steps with more detail.
2.What tools do you have available to implement these steps? This is about tactics. It is at this point I start writing pseudocode.
3.Final implementation. This steps is actually writing out the code. Do research on the commands and functions in the language.
4.Debugging-Go over your code one last time. Follow the logic as closely as you can. Does this code actually accomplish the goals you set forth. And of course is the syntax correct?
5.Run the code. If it doesn’t work go back to any of the previous steps as needed.

1 Like

I am just starting this section and reading through the post are very helpful.
I need lots of help, I question if I can do coding…:frowning:

1 Like