Trouble with Functions


#1

I can figure out whats wrong with my code here. Can someone please help?

Code:
var orangeCost = function(price) {
price * 5;
};

price(console.log("Your total is" price));


#2

You forgot var price and you forgot to call the function using orangeCost(ANY NUMBER HERE) ;

Something like this should work:

var orangeCost = function(price){
var cost= 5*price ;
console.log(cost);
};
orangeCost(50) ;

#3

I have it like this...

var orangeCost = function (price){ 
    console.log(price * 5);
    }
    orangeCost (5);

#4

hi there,
can u help me please

is this a valid code?

var nameString = function (name)
{
return "Hi, I am" + " " + name;
};

console.log = nameString("sherif");

it keeps saying:
Oops, try again. It looks like you didn't print anything to the console! Did you remember to console.log() the result of passing your name to the nameString() function?


#5
console.log = nameString("sherif");

this line overwrites console.log. To fix it you need to instead call the console.log function:

console.log(nameString("sherif"));

and you need to refresh the page to reset console.log to it's original value.


#6

thanks a lot realy for ur help,,

please can you check this

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);

i get this error messge everytime

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.


#7

The return does not need the 2 ( )

If im correct this code should work:

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);

#8

what @amandovledder said. The () are not necessary and your syntax seems to be ok. Probably the "error" is that there is a typo in one of your output messages so copy and paste them from the instruction and you should be able to go on.


#9

thank u
but i have tried without it and gave same error messge


#10

Sure this doesn't cause the problem so it doesn't solves it, just wanted to mention that they are not necessary.
The problem is that there is a typo additional linebreak or something like this in this output message (mentioned this in the last post as well):

return ("You're getting plenty of sleep! Maybe even                       too much!");

probably the many spaces between "even" and "too". So when the test function compares:

expectedOutput === sleepCheck(10)

it fails because they are not equal. But as said nothing to worry you just need to copy and paste the message your syntax is ok.


#11

hey ,, you are a genius,
exactly,, those spaces cause the problem. now all is fine.

appreciate ur great help.


#12

please help..
in the lesson { (Build "Rock, Paper, Scissors"); Both choices are the same! }
what is wrong here:

var compare = function(choice1,choice2){
if (choice1 === choice2){
    return "The resault is a tie"
    }
};

the funny thing is that i copied the code at the -get hint- option and it is same as mine,, still doesn`t work
here it is:
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie";
}
};

error message says:
Oops, try again. Your compare function does not return 'The result is a tie!' when there is a tie.


#13

It's expecting an EXACT STRING RETURN, meaning no spelling errors or anything.

change "resault" to "result"


#14

thank you very much
yes it worked
:smile:


#15

sorry i am so embarrassed now,,
dont know what happened
now stopped again

and honestly i try with their hint but gives same erroe message(Oops, try again. Your compare function does not return 'The result is a tie!' when there is a tie.)

code again is;
var userChoice = prompt("Do you choose rock, paper or scissors?")

var computerChoice = Math.random();
console.log(computerChoice);

if (computerChoice <= 0.33) {
        computerChoice = "rock";
        
} else if (computerChoice <= 0.66)  {
        computerChoice = "paper";
        
    } else { 
        computerChoice = "scissors";
    }

var compare = function (choice1, choice2){
if(choice1 === choice2){
return "The result is a tie" ;
}
};


#16

I believe this is a browser issue. The best method of fixing this issue is to restart your browser. Otherwise if you've already finished this exercise why don't you just move on to the next exercise for now.


#17

You need the ! at the end of your message:

return "The result is a tie" ;

Unfortunately this is wrong in the hint as well.


#18

oh .. i appreciate ur effort.
u r a star
and i need to be more concentrating as programming lang seems too accurate.

thank u very much :smile:


#19

Hi Amadovledder

I am new to coding and I am stuck. these were the instructions: and below is my code, and then the pop up box, telling me what I am doing wrong.

  1. After the closing tag, add another ul element
  2. Inside the
      ..
    tags, add an li element with the text "Sign Up"
  3. Add a second li element with the text "Log In"
  4. Add a third li element with the text "Help"

'
<!DOCTYPE html>
<ul>..</ul><li>Sign Up</li><li>Log In</li><li>Help</li>
<ul><li>Log In</Li><li>Log In</li><li>Help</li>
<li>Susan Krepsova</li>
<li>browse</li>
<li>Help</li> </body>
</html
`
Oops, try again. The ul element should have 3 li elements.

If you can would you help me please?


#20

@susan_k Could you format your code so it shows as the code and not as list. You can do this by selecting your text and pressing CTRL+K