# How Do You Solve the Algorithm Challenges?

Today I started the Basic Algorithm Scripting challenges and when I go to the first lesson, I got stuck almost immediately. And unlike earlier challenges where I could simply look up the documentations, tutorials, or Stack Overflow, this is the one place where I have to face my weakness in coding: problem-solving skills. I’ve ever felt so lost in my entire life.

So here’s my questions:

• How do you approach the coding problems? Do you use some kind of methodology or techniques when working out the problem?
• What do you do when you cannot to solve problem?
• Where do you draw the line between “I’m looking at example samples” vs. “I’m cheating myself out of problem”? **
• Lastly, how long did it take you to improve problem-solving skills?

So far, I only break the problem into smaller chunks and I don’t know where to proceed from here. I mean where do you get the celcius number input?

``````// create a function
function convertToF(celsius) {
let fahrenheit = celsius;
// get the celcius number input
// apply the conversion formula
// return the results
return fahrenheit;
}

// Formula (celsius × 9/5) + 32 = fahrenheit
console.log(convertToF(30)); // 22
console.log(convertToF(-30)); // -22
console.log(convertToF(-10)); // 14
console.log(convertToF(0)); // 32
console.log(convertToF(20)); // 68
console.log(convertToF(30)); // 86
``````

I’m sorry if I ask too many questions at once. You can say this is the biggest obstacle I ever faced since I started learning to code.

** To elaborate, I don’t know if looking at examples like this will cost me the opportunity to develop problem-solving. I don’t intend to copy paste it. Just want to understand how the logic behind this code works.

Do not worry. It will take time but you’ll get there slowly but surely. Practice is the name of the game. You’ll get stuck along the way, but for every obstacle you encounter, your struggle to solve it will increase your problem solving skills.

Coming to your problem. At the top you have a function that takes a parameter. within the function, that parameter will be referred to as celsius. The function is not executed unless you call it.
the function is called from down below in the code

the convertToF(30) calls your function above passing a value of 30 to it. within that function, 30 will be contained in the variable ‘celsius’.

so now what you have to do is to write the code for your function. Let me give you a jumpstart here

when you call your function “convertToF(30)”, the 30 is actually contained in the celsius variable in your function. all you have to do is declare another variable called fahrenheit and assign it to celsius *9/5 +32. After that, the fahrenheit variable will contain the converted value and you can return that.
once the function returns fahrenheit, the program continues from where the function was called giving console.log the returned value of your function.

feel free to ask for clarification as i feel my description might too confusing.

1 Like

Sometimes it helps me to work the problem back to front. In other words, ask yourself what you want to end up with and when you have an answer to that, ask yourself what you need to get that. Rinse and repeat until you’re done.

It’s good that you break down the problem into smaller chunks. Make sure you understand those smaller chunks too. If you can teach the chunks to someone else, good. If not, break it down further or do some studying/research on what you don’t understand. In your particular example, you might need to play around with functions and understand how they work in a different context. A good tool for this might be something like jscomplete or jsfiddle.

2 Likes

I feel like I get the gist of it after examining your code carefully. The only part I will never understand is why parameters is needed in the first place. What’s the difference between variable in parameter & arguments and the normal variable in var,let,const?

On another note, I don’t know how can I hope to reach this solution on my own. I expected I’d write more than one line of codes, since I assumed I will have to write three logical steps by first getting the input of celsius, convert the formula, and return the result. Something like that (please ignore the syntax error):

``````let fahrenheit = celsius; // get the input
celsius = *9/5 +32; // convert Celsius to Fahrenheit
return fahrenheit; // return the results
``````

The final answer seems more simpler than I thought and feel like I’m overthinking myself. My only credit is I put the celsius value inside the fahrenheit variable.

You could think of a function like a box where you put something in one side (argument). The box does something to the input (code in the function) and then the output comes out the other side (return statement).

Parameter and argument are related; parameter is just part of the definition of the function:

``````function myFunction(parameterName){
// do something with parameterName

return parameterName;
}
``````

Argument is what you call the data you pass to a function when you call it:

``````var newData = myFunction(123);
``````

The new variable `newData` will now have whatever `myFunction` returns. Hint: you can write this function with one line.

You can solve this on your own. It will take practice! Don’t worry about writing the most efficient code; write what you need to write to get the desired result and then you can go back and ask yourself “how can I improve this?”.

2 Likes

Before you try to right any code, first write out the steps of how to accomplish the task on paper. If I ask you to take a temperature (given to you in Celsius) and tell me what the the temperature is in Fahrenheit, then you are the convertToF function and the temperature given to you is like the argument to the convertToF function represented by the celsius parameter of the convertToF function. What calculation do you need to perform with celsius to be able to tell me (return from the function) the Fahrenheit value?

1 Like

Finally! An explanation that I can actually understand!

Okay, one last thing. In what situations it makes sense to use function parameters in real-life situations? Of course, I will eventually figure out all user cases on my own, but it will be a huge help if I know the starting point.

Possibilities are endless really. But for example, the function you’re working on (convert C to F) is a good example. I’m sure your phone has a button to change from C to F and back on the weather app. The parameter (temperature) probably comes from an API and the app uses that as an argument for the function `convertToF()`. You could even write a function that toggles the temperature back and forth (hint: it would need two parameters).

1 Like

It could be argued that a function should only make calculations/perform tasks based on what is passed to it and not make references to variables outside of the function’s scope to make calculations/ perform tasks.

1 Like

Now, this is what I’m looking for!

Based on your wonderful explanation, it sounds like I’ll have to build a weather web app to help me solidify the concept of parameters.

The algorithm itself will probably take a long, long time to get comfortable but I have no intention of giving up.

Thank you so much, @RandellDawson, @aliiqbal88 and @ktsubouchi! I truly appreciate your help!!

Just pull from an API that already has an algorithm built in.