Today I Learned Javascript - The While Loop and Problem Solving...My Brain Hurt.

in #til8 years ago (edited)

Today I learn how to do problem solving using loops in Javascript.

I'm sure the devs in here are going to find this really newbish and a little funny having someone writing an article on something so "simple". But to anyone who never coded using computer logic, this is tough! My brain hurt!

Today I had to solve this problem in my class:

Problem Solving with Loop Operations
In the Death Valley National Park, a group of environmentalists have begun a project that will grow the population of Bighorn Sheep. Every month, the population will multiply by four! To stay on top of this explosive growth, the scientists would like a printout of how many sheep will make a new home in Death Valley.

In deathValley.js, use the three existing variables to build a while loop that prints a message for one year’s worth of time. Here’s what the first two lines of output should look like:

There will be 16 sheep after 1 month(s)!
There will be 64 sheep after 2 month(s)!

The While Loop

In order to resolve that problem using Javascript, we must use the while loop.

The basic of while loop is like this

while(*some expression is true*){
 *do this code!*;
}

Here is an example:

var number = 1;
while (number <= 5){
  console.log(number);
  number++;
}

Translation:
So what the above says is: we have a variable called number with a base value of 1. As long as number is inferior or equal to 5, keep executing the loop and print number (we print in the console using console.log). Once you have done so add 1 to the variable called number (by using number++)

Solving the above problem

Problem Solving with Loop Operations
In the Death Valley National Park, a group of environmentalists have begun a project that will grow the population of Bighorn Sheep. Every month, the population will multiply by four! To stay on top of this explosive growth, the scientists would like a printout of how many sheep will make a new home in Death Valley.

In deathValley.js, use the three existing variables to build a while loop that prints a message for one year’s worth of time. Here’s what the first two lines of output should look like:

There will be 16 sheep after 1 month(s)!
There will be 64 sheep after 2 month(s)!

I was starting with those variables:

var numSheep = 4;
var monthNumber = 1;
var monthsToPrint = 12;

I've got to admit, it took me a bit of time to figure that one out. I haven't went to school in a long time and haven't use my brain in that manner before. But I finally figured it out!

var numSheep = 4;
var monthNumber = 1;
var monthsToPrint = 12;
while ( monthNumber <= monthsToPrint ) {
    numSheep *= 4;
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
    monthNumber++;
}

Translation:
As long as the monthNumber is smaller or equal to the amount of monthsToPrint execute the following:

  1. Multiply to number of sheep by 4 (numSheep *= 4)
  2. Print in the console the message
  3. Increase the monthNumber by one

The benefits of explaining what I am learning?

At this point I'm realizing that I might be the biggest benefactor of sharing what I just learned on Steemit. There is a saying by Benjamin Franklin that says:

“Tell me and I forget, teach me and I may remember, involve me and I learn.”

By trying to teach what I learn, I have to make sense of it before being able to pass it on. So I don't know if people actually read this or enjoy it, but it's definitely help me solidify and maybe increase my learning curve.

Let me know what you think on this.

  • Should I bother sharing this stuff on Steemit?
  • Would there be a format that would be more enjoyable to you?

SteemOn!

Sort:  

Awesome! JavaScript can be a lot of fun. Steemit Pond is developed 100% with Javascript, but it's not working atm because piston is down.

Here is a markdown tip for your posts. Use three backticks to begin a code block...

if (you use 3 backticks) {
    // your code blocks will format much prettier
}

if (your post is prettier) {
    // you will get more upvotes :)
}

Perfect.
Get ready to be on top of the trending page!

that comment deserved an upvote :-)

\\what is a backtick please ?\\

lol

The benefits of explaining what I am learning?

This first time you can really say you master a topic is when you need to share it with others, and transmit the associated message. This works for everything.

Thanks @lemouth - I'll keep at it. It does help with my retention so far.

You're making great progress. A very accomplished man is becoming even more accomplished!

@kus-knee (The Old Dog)

Thanks @kus-knee . Do you think there could be a more engaging and interesting way to share this information?

I'm glad you are having fun and learning something new! Knowing a bit about programming, even the basics like this, can be an incredibly useful skill to have. And as @lemouth said, explaining something to other people is a great way to make sure you really understand it yourself.

Should I bother sharing this stuff on Steemit?

Definitely. One, there are more people who don't code here I guess, and there they might find it beneficial, but even more importantly it's logical thinking that you are writing about here as a side-effect, isn't it? And there is never enough of that.
Plus the spirit your display is really enjoyable (in positive sense!) :)

Would there be a format that would be more enjoyable to you?

Can't think of anything different - you are doing good job already :)

Thanks @macius! Your comment is really appreciated. I'll keep going :-)

Well, actually a lot of people might find this interesting as they don't have a solid knowledge about simple things, so, yes, it's worth sharing this kind of posts. Also, while you explain something to someone else, you're unconsciously learning it better.

Did you keep learning? I didn't find any more posts.

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63493.34
ETH 2578.53
USDT 1.00
SBD 2.79