This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Help on function with JavaScript

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone! I am new here at the site, and brand-brand new to JavaScript. So, I should be posting tons of silly and totally beginner questions. I hope y'all don't laugh at them. haha.
Well, I need to write a code using function () where it prompt the user to enter name twice and they run through function Green () function. Then it returns a greeting message with two names in single line. So, this is what I came up with. It works but I feel there is better way. So it would be great if you could help me out!

 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Yes, there are a lot of problems with that code.

You defined a who parameter to the function, but then never use it! Rather, your function code relies on the presence of the global variables, and uses those.

This is really really bad. Not only does it make your function non-portable, but as your JavaScript knowledge grows you will find out that global variables are a big no-no and so relying up them is even a bigger no-no. But that will come with time.

For now, realize that you should not use the variables who1 and who2 in your function. Your function should rely only upon the parameters that are passed to it. (Later, you will find out about a concept called closures that expands this rule -- but that's for later.)

So, compare how you are calling the function on line 14, and how you declare it on line 2. What's wrong there?

 
Bear Bibeault
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, a really big for realizing that just because something works doesn't means that it's good code. That's a really important part of professional programming.
 
Ranch Hand
Posts: 310
18
MS IE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Yuji!

It's great to see that you want to improve your code, despite that it works. The "if it works, it's okay" way of thinking is terrible in software development.

Don't be afraid to ask questions. Everybody was a beginner at some time, we all struggled with the beginners' problems. There is nothing to laugh! :-)


Bear explained you everything. I will add my two cents to help you understand what is wrong.

Think about a function as a device that processes data. You put some data on the input. The device process that input data and gives it back to you, on the output.

As an example, imagine a device that adds sugar to provided cup of coffee. It would be a function addSugar(coffee) in JavaScript:



The mechanism is pretty simple. It takes coffee on input, and gives you back the coffee with sugar.

Now think about your Greet function. It should take names on it's input and give the names back inside a greeting message. But in reality it takes the names that are somewhere else to produce the greeting message. This is very bad. Imagine that you have guests in your home. You want to add to your coffee some sugar using the addSugar function, but it instead of adding sugar to your coffee it adds sugar to your guest's coffee - what if he or she does not like coffee with sugar? It creates a problem. This is why functions shouldn't access anything outside it own body (or object, but it is a knowledge for later)

In summary: your functions should work only with data that you provide as arguments
 
Yuji Ashizawa
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is so cool that there were so many responses as to my post. Thank you so much for everyone who responded to my post! It helped me tremendously.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic