Console.log exercises

Good morning all! I’ve been struggling to figure out Question 5 and wondering if anyone can give me a hint :slight_smile:

. Pyramid-shaped printing.

  1. Create a script that print three lines of stars "*" so that it looks like a pyramid on the screen.
  2. Run the script and make changes until you see that the script works properly.

Thanks!

These first questions are to get used to work on the console.
soo my hint will be to use console.log() u can answer the question just using it.
It is a silly question but it has it purpose

Thanks zero0_cero! I think I got it working. I’ve used this script sequence to get a pyramid of stars in the console:

image

Does that make sense or can it be improved? Thanks for your help!

2 Likes

looks nice to me.
Also u can print the pyramid with out declaring a variable, directly writing it on the console.log.
console.log("*"); like this.
Good work mate :grinning:

1 Like

Problem number 6
I’m having a lot of trouble here. I have the following figured out:
var a = 4
var b = 5
var sum = a + b
From here I am able to say:
console.log = sum
9 prints out
console.log = “The sum of the numbers are:”;
How do I attach the sum to this last line?

Thanks!

console.log("the sum of the numbers are :" , sum);
1 Like

Hi there - i’m taking this course for the second time as i feel i didn’t quite get it the first time… so my question is not related to the above exercises, i’m on page 9 of the eloquent Javascript book. Can someone explain to me the below code please so i can understand properly:

function factorial(n) {
   if (n == 0) {
     return 1;
   } else {
     return factorial(n - 1) * n;
   }
}
console.log(factorial(8)); // → 40320

does this mean that if i input 0 it will return 1, else it will return the inputed number, in this case 8 - so (8-1) * 8

… if so why is the answer 40320 ?

Any explanation would be hugely appreciated - thanks, Jonathon.

Hi @JDW,

What you require right now is a factorial output. Which means if you inputs 5, the output should be --> 5 * 4 * 3 * 2 * 1. Which is equal to 120.

Similarly, the factorial of 8 is equal to 40320.

The code execution goes as follow –

  1. Check if the number is zero. If yes return the last number as 1, If no proceed to the next step
  2. Suppose the number is 3, it went to the else block, then says 3 * fn(2).
  3. Now the input is 2, so it would output 3 * 2 * fn(1)
  4. Now the last input is 1, so it would have the following output - 3 * 2 * 1 * fn(0).
  5. Lastly, the input is 0, so it would then go to the if block and return the final outcome i.e. - 3 *2 *1 *1.
  6. Here the function ends and you are given the final answer which is equal to 6.

Hope this makes it clear for you.

Happy learning!

Thanks Malik - you are helping massively !
Now it makes sense !

1 Like

Just a quick question about number 5, I got the same result as the solutions page but I wrote it like this:

console.log(" * \n *** \n*****")

instead of like this:

console.log(" * “);
console.log(” *** “);
console.log(”*****");

just wondering if there was any inherent problem with doing it this way that would come up later, or if it is maybe just not the standard way of doing it?

The point of the exercise is for you to learn how loops work and how to use it to iterate over a sequence of strings or arrays. By using console.log directly, you are missing the core essence of learning the loop functionality.

Hope this makes sense. Happy Learning!