a swirl

Basic Javascript Function Tasks

Open the console to view the results or check out the accordion items below.


Code

function outputMessage(){
   console.log("This is a message");
}
                                

Test code

outputMessage();
                                

Results

This is a message              
                                

Code

function createName(firstname, lastname) {
    fullName = firstname + ' ' + lastname;
    console.log(fullName);
}
                                

Test code

createName('Jim','Stott');
var fname = 'Jane';
var lname = 'Leaver'
createName(fname,lname);
                                

Results

Jim Stott
Jane Leaver              
                                

Code

function createFullName(firstname, lastname) {
  return firstname + ' ' + lastname;
}
                                

Test code

var fname = 'Jane';
var lname = 'Leaver'
var fullName = createFullName(fname, lname);
console.log(fullName);
console.log(createFullName(fname, lname));
                                

Results

Jane Leaver
Jane Leaver              
                                

Code

function wearacoat(temp) {
  if (temp <10)
      return 'You should wear a coat when the temperature is '+temp;
  else
      return 'You should not wear a coat when the temperature is '+temp;
}
                                

Test code

var temperature = 15;
console.log(wearacoat(temperature));
temperature = -5;
console.log(wearacoat(temperature));
                                

Results

You should not wear a coat when the temperature is 15
You should wear a coat when the temperature is -5              
                                

Code

function wearACoatExtra(temp){
  strMessage = 'You don\'t need a coat';
  if (temp < 0) {
      strMessage  = 'Don\'t go outside';
  }
  else if (temp < 10) {
      strMessage = 'You need a coat and hat';
  }
 else if (temp < 15) {
      strMessage = 'You need a coat';
  }
 
  return strMessage;
}
                                

Test code

console.log(wearACoatExtra(-4));
console.log(wearACoatExtra(9));
console.log(wearACoatExtra(14));
console.log(wearACoatExtra(18));
                                

Results

Don't go outside
You need a coat and hat
You need a coat
You don't need a coat              
                                

Code

function wearACoatExtraExtra(temp){
  var strMessage = '';
  if (temp < 0) {
      strMessage  = 'Don\'t go outside';
  }
 else if (temp < 10) {
      strMessage = 'You need a coat and hat';
  }
  else if (temp < 15) {
      strMessage = 'You need a coat';
  }
   if (!strMessage){
      strMessage = "You don't need to wear a coat";
 }
 
  return strMessage;
}
                                

Test code

console.log(wearACoatExtra(-4));
console.log(wearACoatExtra(9));
console.log(wearACoatExtra(14));
console.log(wearACoatExtra(18));
                                

Results

Don't go outside
You need a coat and hat
You need a coat
You don't need to wear a coat