Yuekai's favicon

Yuekai's Portfolio and Blog

My learning journey of Problem Solving

Damn! I was blocked on a simple problem

I stuck in question 7 'STRETCH MATERIAL' in the 4-gradebook challenge for nearly an hour. The question asked me to complete a function which takes 3 parameters: gradebook(it is an empty object), students(it is an array) and scores(it is an array). This was my first answer:


        function enterScores(gradebook, students, scores) {
          for (let i = 0; i < students[i]; i++) {
            gradebook[students[i]] = {}
          }
          for (let i = 0; i < students[i]; i++) {
            gradebook[students[i]]['testScores'] = scores[i]
          }
        }
      

If you are good with details and have deep understanding of for loop, I think you have already figured out where the problem is, LOL. When I ran the test in the terminal, it returned the error message: 'TypeError: Cannot read properties of undefined' with an arrow pointing to the position under the code ['testScores'] in the second loop. I searched for the error message on Google, and there were many examples explaining how to solve this issue. However, none of them were the same or similar to my code. I believe the problem lies in the incorrect syntax of the code on this line:


        gradebook[students[i]]['testScores'] = scores[i]
      

I tried other syntaxes of this code:


        gradebook.students[i].['testScores'] = scores[i]
      

        gradebook.students[i].testScores. = scores[i]
      

Yup, none of them could solve the problem. I was stuck in this rabbit hole. Then, I took a break and decided to use the rubber ducky method to review my solution and the code. The first time I talked to the duck, I realized that the error was not in this code, and the problem should be somewhere else. I wrote console.log in each for loop and read the logs in the console. Finally, I found the silly mistake inside the conditions in the for loop. The condition i < students[i] was incorrect and should be i < students.length. Here is the correct anwser:


        function enterScores(gradebook, students, scores) {
          for (let i = 0; i < students.length; i++) {
            gradebook[students[i]] = {}
          }
          for (let i = 0; i < students.length; i++) {
            gradebook[students[i]]['testScores'] = scores[i]
          }
        }
      

After solving this problem, I did feel very sheepish for making such a silly mistake. However, on the other hand, I was proud of myself for trying new and easier problem-solving techniques to address the issue. I also learned that I should try different techniques to solve a problem instead of relying on just one approach. Moreover, I realized the importance of taking a break and coming back to the problem with a fresh perspective.

Yeah! I elegantly solved a problem

The first time I felt I elegantly solved a problem was in the 3-super-fizzbuzz challenge. The challenge required writing two functions: one that would return the number passed to it, but if the number was divisible by 15, it would return the string FizzBuzz; if the number was divisible by 5, it would return the string Buzz; and if the number was divisible by 3, it would return the string Fizz. The other function would take an array of numbers as input, loop over the array, and return a new array applying the fizzbuzz rules.

To solve this problem, I used the rubber ducky method, talking myself through what I needed to do. For the first function, since there were four conditions, I decided to use an if - else if - else statement to implement it. For the second function, I used a for loop to iterate over the numbers in the array and called the first function to apply the fizzbuzz rules. Then, I wrote pseudocode to demonstrate my plan:


        //function one
        //function with a number parameter
        //if statement: the number is dicisible by 15, return the string FizzBuzz
        //else if statement: the number is dicisible by 5, return the string Buzz
        //else if statement: the number is dicisible by 3, return the string Fizz
        //else statement: just return the number parameter

        //function two
        //function with an array of numbers
        //declare an empty array
        //for statement
        //call function one with a parameter of the number in the array
        //add the returned number into the empty array
        //return the new array
      

Using these two methods, generating a plan and organising the solution before writing actual code, felt like a game-changer. It made the programming process feel easier and more manageable. I learned that early planning and organising ideas and solutions can save a lot of work and make the entire task more effective. It's a valuable lesson in terms of improving productivity and efficiency in programming.

How do I feel using the problem-solving techniques or processes?

What can I try differently when I was reluctant to ask for help?

I haven't asked for help in the foundation course yet for two reasons. Firstly, the most challenging questions have already been asked and solved in Discord channel, so I have been observing and learning from those discussions. Secondly, I tend to prefer seeking help towards the end, after I have exhausted all possible methods on my own. In the case of question 7 'STRETCH MATERIAL' in the 4-gradebook challenge, I actually identified the error just before deciding to ask for help in the Discord channel. Since it was an individual challenge, I felt it was acceptable to hesitate in seeking assistance. However, I recognise the need to change this approach and be more proactive in asking for help in the future, especially when working on larger group projects, as it can be more effective.

08 JULY 2023

Contact Me

Keen to share your thoughts with me?
Feel free to contact me through email or social media.

Email icon Github icon Linkedin icon