Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Question help needed

help urgent

Best Answer WingedPanther73, 09 June 2014 - 12:00 PM

When I copied/pasted the code to a .html document and loaded it, my console said "invalid character" for your

d = a – b * c;

It looks like you typed the code in something like word instead of notepad. When I replaced the - character, it runs and produces:

b now contains: apple and d contains: 1
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 kevinlfc1000

kevinlfc1000

    CC Regular

  • Member
  • PipPipPip
  • 25 posts

Posted 09 June 2014 - 09:55 AM

What is the value of the variables b and d after the following JavaScript code is executed?
<script type="text/javascript">
var a = 5, b = 2, c = 2, d = 1;
var fruit = new Array();
fruit[0] = "pear";
fruit[1] = "apple";
fruit[2] = "guava";
fruit[3] = "orange";
fruit[4] = "grape";
fruit[5] = "avocado";
fruit[6] = "cherry";
if( b <= c ){
d = a – b * c;
b = fruit[d];
} else {
d = a;
b = fruit[c];
}
alert( "b now contains: " + b + " and d contains: " + d);
</script>
 


#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 09 June 2014 - 10:05 AM

This sounds suspiciously like a homework question.

Tell us, where are you getting lost? This relatively short piece of code should be easy to manually step through. What do you perceive the result being of the first conditional test?
if( b <= c ){
That will tell you whether the first block of code gets executed or not, which will reveal what assignments are being made, and what the final outcome will be. In the case above, b has been initialized to 2, and c has been initialized to 2. How would that statement evaluate?

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 09 June 2014 - 10:20 AM

Have you executed the code?


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 kevinlfc1000

kevinlfc1000

    CC Regular

  • Member
  • PipPipPip
  • 25 posts

Posted 09 June 2014 - 10:50 AM

please help by telling me how to execute the code as what should be put in the header style and body.


here is how i have done it and it is not executing can you chech it

 

 

<!Doctype HTML>
<html>
<head>
</head>
<Body>
<script language="javascript">
var a = 5, b = 2, c = 2, d = 1;
var fruit = new Array();
fruit[0] = "pear";
fruit[1] = "apple";
fruit[2] = "guava";
fruit[3] = "orange";
fruit[4] = "grape";
fruit[5] = "avocado";
fruit[6] = "cherry";
if( b <= c ){
d = a – b * c;
b = fruit[d];
} else {
d = a;
b = fruit[c];
}
alert( "b now contains: " + b + " and d contains: " + d);
 
</script>
</Body>
</HTML>

nothing is happening can somebody help

??????????



#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 09 June 2014 - 12:00 PM   Best Answer

When I copied/pasted the code to a .html document and loaded it, my console said "invalid character" for your

d = a – b * c;

It looks like you typed the code in something like word instead of notepad. When I replaced the - character, it runs and produces:

b now contains: apple and d contains: 1

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 kevinlfc1000

kevinlfc1000

    CC Regular

  • Member
  • PipPipPip
  • 25 posts

Posted 09 June 2014 - 12:56 PM

thank you very much got the error was a typing mistake






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download