190k views
5 votes
Put the text "I love Lucy" in a label. Add a button btnChange. Add another lable lblNumber

Use string functions to do the following separately when clicking on the button. (use the label text changed event and a string function to show in the lblNumber the number of characters of the string displyed in the first label when the chnages occur) :
1. Remove "I" from the label text
2. Substitute "I" with "You"
3. Add "very much" at the end
4. Substitute "love" with "hate"

1 Answer

3 votes

Final answer:

To perform the given tasks, you can use JavaScript string functions to manipulate the label text. You can remove a specific character, substitute one word with another, add text at the end, or substitute one word with another in the label text.

Step-by-step explanation:

To accomplish the given tasks, you can use string functions in JavaScript. Here's how you can do each of the tasks:

  1. Removing "I" from the label text:

    const labelText = document.querySelector('#labelId').textContent;
    const modifiedText = labelText.replace('I', '');
    document.querySelector('#labelId').textContent = modifiedText;
  2. Substituting "I" with "You":

    const labelText = document.querySelector('#labelId').textContent;
    const modifiedText = labelText.replace('I', 'You');
    document.querySelector('#labelId').textContent = modifiedText;
  3. Adding "very much" at the end:

    const labelText = document.querySelector('#labelId').textContent;
    const modifiedText = labelText + ' very much';
    document.querySelector('#labelId').textContent = modifiedText;
  4. Substituting "love" with "hate":

    const labelText = document.querySelector('#labelId').textContent;
    const modifiedText = labelText.replace('love', 'hate');
    document.querySelector('#labelId').textContent = modifiedText;

User Shuvo Sarker
by
7.5k points