Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Explain Code Of Javascript

javascript

Best Answer Vaielab, 08 June 2014 - 05:59 AM

See my comments

var text = prompt("What is this ?"); //Ask the question "What is this ?"
var firstChar = text.indexOf("World War II"); //Find the position of the text "World War II"
if (firstChar !== -1) { //If "World War II" if found in the text
text = text.slice(0, firstChar) + "the Second World War" + text.slice(firstChar + 12); //Replace "World War II" with "the Second World War"
}
alert (text); //display the text either the original text, or the text with "the Second World War" insted of "World War II"
Go to the full post


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

#1 B13

B13

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 08 June 2014 - 03:16 AM

I am new learner of JScript. I cannot understand below codes. Can anyone explain line 3 and 4 in javascript ?
 

 

 | Copy Code

var text = prompt("What is this ?");
var firstChar = text.indexOf("World War II");
if (firstChar !== -1) {
text = text.slice(0, firstChar) + "the Second World War" + text.slice(firstChar + 12);
}
alert (text);


:confused:


#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 08 June 2014 - 05:59 AM   Best Answer

See my comments

var text = prompt("What is this ?"); //Ask the question "What is this ?"
var firstChar = text.indexOf("World War II"); //Find the position of the text "World War II"
if (firstChar !== -1) { //If "World War II" if found in the text
text = text.slice(0, firstChar) + "the Second World War" + text.slice(firstChar + 12); //Replace "World War II" with "the Second World War"
}
alert (text); //display the text either the original text, or the text with "the Second World War" insted of "World War II"

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#3 B13

B13

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 08 June 2014 - 07:58 AM

Could you please explain Each part of the line ?
 

text = text.slice(0, firstChar) + "the Second World War" + text.slice(firstChar + 12); 

Edited by najmul, 08 June 2014 - 08:00 AM.

:confused:


#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 08 June 2014 - 08:25 AM

slice cuts a segment of a string, and you tell which character you want to start with, and which to end. For example, given the following string:

 

"012345678901234"

 

this: text.slice(2,4)

 

will give you "23"  which are characters 2 and 3, starting from 0



#5 B13

B13

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 08 June 2014 - 08:36 AM

Would you please explain the last 2nd and last segment of the code :
 

 "the Second World War" + text.slice(firstChar + 12); 

:confused:


#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 08 June 2014 - 09:13 AM

the second part, slice will sum the values of firstchar + 12, and will cut the string from there (from that character position) to the end.

Since the second parameter of slice is optional, and if you don't provide it, the cut will go to the end of the string.



#7 B13

B13

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 08 June 2014 - 06:17 PM

Thanks @black Rabbit for explanation .Its really helped me  


:confused:





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