TP1 Strings Type Castings Exercices

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

TP 1: STringS manuPulaTion – numBErS & TYPE CoErCion :

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

• Displaying the Result:


console.log("Hello, " + userName); // Displays the result in the console
alert("Hello, " + userName); // Displays the result in an alert box

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.

Type Coercion and Type Casting:


Exercise 1: Convert String to Number
Ask the user to input a number as a string (e.g., "42"). Convert the input to a number, add 10
to it, and display the result.
Exercise 2: Boolean Conversion
Ask the user to input a value (e.g., "", "Hello", 0, 1). Convert the input to a boolean and
display whether it's true or false.
Exercise 3: Number to String Conversion
Create a function that takes a number and converts it to a string. Then return a sentence
that includes the number in string format.
Exercise 4: Array to String Conversion
Create an array of words (e.g., ["JavaScript", "is", "fun"]). Convert the array into a single
string where each word is separated by a space.
Exercise 5: Parse Int and Float from String
Ask the user to enter a string with both an integer and a decimal number (e.g., "42.58"). Use
parseInt and parseFloat to extract the integer and the decimal number separately and
display them.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy