TP1 Strings Type Castings Exercices
TP1 Strings Type Castings Exercices
TP1 Strings Type Castings Exercices
Note That : To take user input in JavaScript, you can use the prompt() function,
and to display the result, you can use either console.log() or alert().
Examples:
• Taking User Input:
let userName = prompt("Enter your name:");
Strings Exercices:
Exercise 1:
Ask the user to enter a word and an index number, then display the character at that index.
Exercise 2:
Ask the user to enter a sentence, then display the sentence in both uppercase and
lowercase.
Exercise 3:
Ask the user to enter a sentence with extra spaces at the beginning and/or end, then display
the sentence with spaces removed.
Exercise 4:
Ask the user to enter a sentence and a word. Check if the sentence includes the specified
word.
Exercise 5:
Ask the user to enter a sentence and a word. Display the position of the first occurrence of
the word in the sentence.
Exercise 6:
Ask the user to enter a sentence and two numbers. Extract and display a part of the
sentence using these numbers as the start and end positions.
Exercise 7:
Ask the user to enter a sentence, a word to replace, and a new word. Display the sentence
with the specified word replaced.
Exercise 8:
Ask the user to enter a sentence and a character to split the sentence by. Display the
resulting array.
Exercise 9:
1. Ask the user to enter a sentence.
2. Perform the following operations:
o Remove any leading and trailing whitespace.
o Convert the entire sentence to lowercase.
o Check if the sentence includes the word "JavaScript".
o Replace the word "JavaScript" with "JS" if it exists in the sentence.
o Display the position of the first occurrence of the letter "e".
o Extract a part of the sentence (from index 5 to 15).
o Split the sentence into words and display each word on a new line.