Thursday, April 29, 2021

Javascript Practice 08: JS Hero - Function calls

 
Question: 

1. Define a function greet returning the value 'Haydo!'.
2. Declare a variable salutation. Call the function greet and assign the result of the call to the variable salutation.




Answer: 

function greet() {

return 'Haydo!'; 

};

var salutation = greet();


> Declared a function greet

> Called the greet function and stored the returned value in the variable salutation

> salutation now has a value of 'Haydo!' 

Javascript Practice 07: JS Hero - Multiple functions

 
Question: 

Define two functions. The first function a should return 'Hello a!' and the second function b should return 'Hello b!'.




Answer: 

function a () {

return 'Hello a!';

}; 

function b () {

return 'Hello b!';

}; 



JS Function Syntax: 

Keyword: function

Name of the function: hello

Parameters within Parentheses: ()

Body within Curly Brackets: { return 'Hello World!'; };

Note: 

return statement determines the value the function returns

Always use semi-colons in your code to avoid errors

Javascript Practice 06: JS Hero - Functions

Question: 

Define a function hello that returns 'Hello world!'.





Answer: 

function hello () {

return 'Hello World!"; 

}; 


JS Function Syntax: 

Keyword: function

Name of the function: hello

Parameters within Parentheses: ()

Body within Curly Brackets: { return 'Hello World!'; };

Note: 

return statement determines the value the function returns

Always use semi-colons in your code to avoid errors






 

Javascript Practice 05: JS Hero - Assign variables


Question: 

Which value does x have after execution of the following code?

let x = 'Laurel';
let y = 'Hardy';
let z = y;
y = x;
x = z;





Answer: 

'Hardy'

x = Laurel

y = Hardy 

z = Hardy

y = Laurel 

Therefore x = z = Hardy

Declaring and Reassigning Variables can be solves if questions are broken down and simplified.

Javascript Practice 04: JS Hero - Reassignment


Question: 

Which value does x have after execution of the following code?

let x = 'Tic';

x = 'Tac';

x = 'Toe';




Answer: 

'Toe'

Declared a variable using let.
You can reassign a new value to a variable using the = operator as shown above. 

Wednesday, April 28, 2021

Javascript Practice 03: JS Hero - Several variables


Question: 

Declare a variable flower and assign it the value 'rose'
Declare a second variable tree and assign it the value 'maple'.




Answer: 

You can create variables using var, let and const. 

var flower = 'rose'; or
let flower = 'rose'; or
const flower = 'rose'; 

var tree = 'maple'; or
let tree = 'maple'; or
const tree = 'maple'; 

All of the options will work and are OK to use. 

Javascript Practice 02: JS Hero - What is x?


Question: 

Which value does x have after execution of the following code?

let x = 'Geeta';





Answer: 

x will have the value of 'Geeta' (with the quotes) - which is a string. 
x will not have the value of Geeta (without the quotes)

Javascript Practice 01: JS Hero - Variables


Question: 

Declare a variable firstname and initialize it with the value 'Lata'.





Answer: 

This is a fairly simple exercise. 
> Can declare variables in 3 ways. 
> var, let & const

var firstname = 'Lata'; or (pre - ES6 Variable) (function scoped)
let firstname = 'Lata'; or (ES6 Variables) (block scoped)
const firstname = 'Lata'; (ES6 Variables) (block scoped)

All the 3 options will work. 
They have different properties.  
You can refer to MDN Webdocs or W3Schools for further information. 

My First Post


This is my first post on this blog !!

This blog will provide solutions to Javascript exercises.  I will try and explain the logic behind each and every code. 

My intent is to deepen my learning of Javascript (and maybe other programming languages) by trying to simplify coding. This exercise will (I assume) benefit me and maybe others who stumble upon this blog and follow me.

Will keep posting daily so lets get it started !!