# A functions quiz which I have struggled a bit with. Any help?!

There you go, guys… I tried different times, but in each one there was something seems broken!

A code newbie life haha!

This is for another course? Is it part of the certificate requirements, or merely an exercise? If you provide a link, I’ll gladly help you with an exercise.

What have you tried?
We would not give you the answer, but if you show what have you tried we could point out what’s wrong and hint on how to fix it

It’s just a normal quiz in an introductory free JS course on Udacity. No certificates included btw.

Well, hope it would not blow up your computers!

``````function laugh(x) {

var num = "ha";
for(var lol = 0; lol <= 3; lol++) {
num *= lol;
}
return num;
}

console.log(laugh(3))

//--------------------------------------

function laugh(x) {

var num =;
for(num = 0; num <= 3; num++) {
num *= x;
}
return num;
}

var lol = "ha";
console.log(lol * laugh(3) + "!");
//--------------------------------------

function laugh(x) {

var num =;
for(num = 0; num <= 3; num++) {
num *= x;
}
return num;
}

var lol = laugh(3);
console.log(lol + "!");
``````

You can’t use multiplication with a string

You can add strings together with the `+` operator

Your variable naming way is strange (you called `"ha"` as `num`, but when you had to use a number as variable you called it `lol`)

As first thing try creating pseudo code as detailed as possible as a starting point

You can also try with the first few lessons of te JavaScript curriculum in FreeCodeCamp, it would be enough to make you able to solve this problem

Don’t do this, if you are just declaring a variable don’t use the `=` assignment operator, or if you use it actually assign something to the variable.

1 Like

You may be subject to an “off-by-one” error in your for loops. If you think about how we count numbers, we start from “1, 2, 3 … num”, but in computer science, we often count “0, 1, 2 … num-1”. This isn’t an accident - it has to do with C-type languages and addressing memory. Notice that in both cases we count the same number of numbers, it’s just that in computing, we often count from `0 to num-1`. Your for loops go from [0 to num], which is actually num+1 times.

You also have an issue with variable naming/assignment. You can declare a variable without assigning it a value, and in JS, its value will be `undefined`, but it should be like: `var num;`, not `var num=;`. This wouldn’t blow up my computer, it just shouldn’t compile and run.

Also, notice the assignment states that the function `laugh()` should return a string that you can then feed into `console.log()`. You shouldn’t return a number.

Thanks! I planned to go for it after finishing this course.

Thanks for the tips!

1 Like

Also, this is a point of style/keeping things straight. Instead of calling the parameter you are passing to `laugh()` `x`, you should call it `num` like the assignment says: `function laugh(num)`. The idea is that the code is calling the function `laugh` and “passing in a value” which we store in an internal variable in the function, which we give a label, `num`. From then on, if we wanted to know the `num`-ber of times to say ha, we can just look up the value stored in `num`, like for example, knowing how many times to execute our `for` loop.

Oh, guys! This way It works.

``````function laugh (lol) {
var num = "";

for(var x = 1; x <= lol; x++) {
num += "ha"
}

return num + "!";
}

console.log(laugh(3));

// Woohoo!!! It F works!!! ... And 'F' stands for <em>'Finally'</em> FYI. 😜😁

``````

`"ha".repeat(3) + '!'; // works too.. much less typing.`

1 Like

Yeah, but did not face it this point yet!

1 Like

Congratulations!

Did you checked FreeCodeCamp lessons?

1 Like

For a newbie, you are doing it the way the lesson asks, which means that you are learning the lesson about loops: How to initialize your output, loop and alter it, and terminate the output processing based on what the input is. When it comes time to learn the “tricks” of Javascript, I recommend Mozilla Developer Network. Don’t worry, you’ll get there. May I suggest one change for legibility? If you swapped the names of the variables `num` and `lol`, the code would be clear to anyone. You literally have a variable called `num` that doesn’t hold a number.

1 Like

Nope! I kept on and left this quiz for a while, then through the course as moving forward, I was thinking about the possible ways to manipulate it right, all of a sudden! I found one!

Awesome! But I agree with @vipatron : naming variable consistency is greatly important, you will thank yourself if you ever return back to your own code.

Use descriptive names for your variables, that give you an idea of what that variable does at first glance

The `num` variable right here does not mean an actual number but the number of “ha” as the quiz asked. If you took a look I put an empty double quote right after the equal sign, it may seem a clear hint that it is going to be a string not a number from a data-types perspective. For `lol`, I did not give it much time thinking what name it should have to be accessible, actually, I was curious the most about solving the quiz in the first place lol

Dude, I’ve programmed AIs in college before I went off to pursue grad school in biology. I am straight-up telling you your naming is confusing. Even better than `lol` would be `result` or `output`, which tells me instantly that we are building the value we will return. As for a variable named `num`, if it didn’t have a number in it, I would put soap in the coffee of the person who did that (Not really, but you get the idea of how much naming precision matters to programmers).

1 Like