10) Global vs Local var - alternate solution


#1



Hi everyone,
I understand this exercise and the mechanics but I was wondering if there was an alternate way to complete this exercise.

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/2/2?curriculum_id=506324b3a7dffd00020bf661

This is the correct answer:

var my_number = 7; //this has global scope

var timesTwo = function(number) {
    var my_number = number * 2;
    console.log("Inside the function my_number is: ");
    console.log(my_number);
}; 

timesTwo(7);

console.log("Outside the function my_number is: ");
console.log(my_number);

And this is my other solution:

var my_number = 7; //this has global scope

var timesTwo = function(number) {
    var my_number = number * 2;
    console.log("Inside the function my_number is:" + " " + my_number);
}; 

timesTwo(7);

console.log("Outside the function my_number is:" + " " + my_number);

Was this solution not used because of spacing or is it just plain wrong? I'm sorry if this is a repeat question, I couldn't find it in the forum. Thank you!


#2

@dooshadoosh,
Apart from an other constellation of the console.log() statement
your First and Other solutions are the same....


#3

It wouldn't let me submit the alternate solution though which is why I posted this topic. It hasn't got any mistakes in it, right?


#4

@dooshadoosh,
Your code is Ok,

But this exercise tries to introduce the Global / Local Scope to you
read through the Posts
https://discuss.codecademy.com/t/global-vs-local-variables/45849/4?u=leonhard.wettengmx.n


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.