Functions in Javascript

Hey guys!
Short question to help me understanding.

I was typing the code where Ivan was explaining how to put 2 or more variables in a function.

When I typed the number of Dogs in this case it wouldn’t work. Only after setting it under quotation marks.

How come?

1 Like

Edit: I literally didn’t change anything. Now it works. Very weird.

Ok, glad it worked out.

2 Likes

I don’t know whether to post here or to create my own topic, but i will just post here since i am on this part of the lecture.

Firstly i created 2 functions like so:

  <script>

  function greet (name, number)
  {
    document.write("<h1> Hey there " + name + "your number is " + number + "</h1>");
  }
  greet("Ted", 12);
  
  function greetTwo (name)
  {
    document.write("<h1><i> Hey there " + name + "</i></h1>");
  }
  greetTwo("Victoria");
  
  </script>

The result is simple:


Hey there Tedyour number is 12

Hey there Victoria


Now i tried experimenting and named the renamed the ‘greetTwo’ function to ‘greet’. Basically both functions are now with the same name ‘greet’.

  <script>

  function greet (name, number)
  {
    document.write("<h1> Hey there " + name + "your number is " + number + "</h1>");
  }
  greet("Ted", 12);

  function greet (name)
  {
    document.write("<h1><i> Hey there " + name + "</i></h1>");
  }
  greet("Victoria");

  </script>

What i was trying to do is see how the code would execute. In theory i thought it would firstly execute the first ‘greet’ [greet(“Ted”, 12);] and i would get 'Hey there Ted your number is 12’

Then the second ‘greet’ would be overwritten, leaving the ‘Hey there Ted your number is 12’ as it is. Then using the [greet(“Victoria”);] command i would call out the newly created ‘greet’ function without a number. ‘Hey there Victoria’. But the result i got is weird in my opinion.


Hey there Ted

Hey there Victoria


This is what i got.

What is weird is this is how i imagined the code going:

function greet (name, number)
greet("Ted", 12);
function greet (name)
greet("Victoria");

What i got in theory is:

function greet (name)
greet(“Ted”, 12);
greet(“Victoria”);

So from what i understand, the code already knows the functions in the whole code. So when i call those functions, the last overwritten function (from the whole code) would be executed. So if i was to overwrite ‘greet’ a million times, the program would still use the last ‘greet’ that was overwritten for the whole code.

The example of my last statement:

  function greet (name) {document.write("HEY THERE " + name); }
  greet("Ted");
  function greet (name) {document.write("<p> HOW YOU BEEN " + name); }
  greet("Lacy");
  greet("Lacie");
  greet("Lacyeyeye");
  function greet (name) {document.write("WASSUP@@@@ " + name); }
  greet("Onut");
  function greet (name) {document.write("EYO MA FRIEND" + name); }
  function greet (name) {document.write("<p> hey " + name); }
  greet("Micheal");

The result of this:


hey Ted

hey Lacy

hey Onut

hey Micheal


Only the last overwritten ‘greet’ was done. But it executed all previous calls of ‘greet’ in the code (like Lacy, Lacie, Onut).

I guess this is what you call a global function, but knowing that it would work like this is very interesting.

Hi guys,
I have just watched ABSTRACTION and Returning Values from Functions JavaScript video, where Ivan wrote a multiplication function.
When I have tried to pass a negative number to variable “a”, in my code “multiplier”, it returned 0 as a result.
This is the video:


The code he wrote:

function add(x,y){
return x + y;
}
function multiplication(a,b){
var toReturn = 0;
for(var counter = 0; counter < b; counter++){
toReturn = add(toReturn, a);
}return toReturn
}
console.log(multiplication(2,-10))

I have tried to tweak it a bit to make it work with negative numbers.
Could any of you check if my code makes sense, and give advice on how to might write it differently, or in a smarter format, please?
My code looks like this:

function multiply(number, multiplier){
var toReturn = 0;
for(num = 0; num < multiplier; num++){
var toReturn = toReturn + number;
}
if(multiplier < 0){
for(num = 0; num > multiplier; num–)
var toReturn = toReturn - number;
}
return toReturn;
}
console.log(multiply(21,-3));

Also, is there any better way to pass code into the conversations here in Toshitimes to display it with the indentations?

Thanks a lot

Zoltan

@ivga80 Edit. Please format the code with preformatted text.