Declaring Functions


#1

// Write your foodDemand function below.
// Last hint: In your reusable block of code, end each line
// with a semicolon (:wink:
var functionName = function foodDemand ("food"); {
console.log("I want to eat" + "foodDemand");}



Replace this line with your code.


#2

Hi your function should look like that

var foodDemand  = function() {
    #here goes  your code 
}

You shouldn't put the " " around the argument food.


#3

thanks can you help with this one?
You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!

You want to declare a function that calculates the cost of buying 5 oranges.
You then want to calculate the cost of the 5 all together.
Write a function that does this called orangeCost().
It should take a parameter that is the cost of an orange, and multiply it by 5.
It should log the result of the multiplication to the console.
Call the function where oranges each cost 5 dollars.

this is what i have:
var function orangeCost(cost * 5);
{console.log(orangeCost);}

orangeCost("5")


#4

First your function should look like that and should have as argument price Without " "

var orangeCost = function(price) {
  #here goes your code 

}

Inside the function you should do the math

return  price  * 5

So that when you call the function with the number 5 Without the " " around it.


#5

how do i define price


#6

Just by calling the function orangeCost() like that

orangeCost(5)

#7

var quarter function {return number/4;}

if (quarter(9) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}

Im not sure if i am defining the function correctly. Its supposed to go in parenthesis?


#8

HI you function quater should be like that

var quater = function(number) {
    #here goes the return statement 
}

and should that as argument number


#9

I'm still getting a syntax error??

var quarter function (number);
{return number/4}

if (quarter(9) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}


#10

var quarter function (number)
{return number/4;}

if (quarter(12) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}

I fixed it to this but its unexpected token function


#11

nvm was missing my = sign


#12

var sleepCheck = function(numHours)
{
if (numhours > 8){
return ("You're getting plenty of sleep! Maybe even too much!");};
else {
return ("Get some more shut eye!");
};
}

sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

do you have any idea whats wrong with this?


#13

var sleepCheck = function(numHours)
{
if (numHours >= 8)
{return "You're getting plenty of sleep! Maybe even too much!";}
else {
return "Get some more shut eye!";
}
}

sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

so i have this but its saying Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator.


#14

Hi first you should remove the ; after your closing if ans else statement

var sleepCheck = function(numHours) {
  if (numHours >= 8) {
    return ("You're getting plenty of sleep! Maybe even too much!");
  };<= this one
  else {
    return ("Get some more shut eye!");
  }; <== this one too
}

then here

if (numhours > 8)

you change numhours to numHours and put >= instead of just >


#15

I just ran it work, try to refresh the page


#16

weird still not working for me but thanks glad i know its correct before moving on


#17

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice)
if (computerChoice //between 0 and .33// ) {computerChoice = "rock";}
if else (computerChoice //between .34 and .66// {computerChoice = "paper";}
else {computerChoice = "scissors";}

So I am wondering how to write a condition that says computerChoice is between 0 and .33 but not sure how to do that?


#18

do it like that

if (computerChoice <= 0.33)

else if (computerChoice <= 0.34 && computerChoice <= 0.66)

#19

A more easier way to write the conditions is to focus on the two easiest ones. Such as the first and last one. So you would write something like this:

if(computerChoice < 0.34) <---- This basically means any number until 0.34 not including 0.34
{
}
else if(computerChoice > 0.66) <----- This basically says anything over 0.66 but not including 0.66
{
}
else
{
}

So you do not have to think about the middle between 0.34 and 0.66