Practice Set on Strings in JavaScript

 //Chapter - 4 : Practice Set

//Question 1 : What will the following print in JavaScript ?
//console.log("har\"".length);
let str = "har\"";
console.log(str.length);

//Question 2 : Explore the included, startsWith & endsWith functions of a string
const sentence = "The quick brown for jumps over the lazy dog.";
const word = "fox2";
console.log(sentence.includes(word));   //.includes() always return True or False
console.log(`The word '${word}' ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);
console.log(sentence.startsWith("The"));
console.log(sentence.startsWith("Sat"));
console.log(sentence.startsWith("The", 0));
console.log(sentence.startsWith("The", 1));
console.log(sentence.endsWith("dog."));
console.log(sentence.endsWith("Sat"));
console.log(sentence.endsWith("dog.", 44));
console.log(sentence.endsWith("dog.", 40));

//Question 3 : Write a program to convert a given string to lowercase
const sentenceTwo = "HELLO WORLD, HOW are YOU ?";
console.log(sentenceTwo.toLowerCase());
console.log(sentence.toUpperCase());

//Question 4 : Extract the amount out of this string "please give Rs 1000"
let str2 = "Please give Rs 1000";
let amount = Number.parseInt(str2.slice(15));
console.log(amount);
console.log(typeof amount);

//Question 5 : Try to change 4rth character of a given string were you able to do it ?
let friend = "Deepika";
friend[3] = "R";
console.log(friend);    //"friend" is not changed, because string is immutable



OUTPUT :
4 false The word 'fox2' is not in the sentence true false true false true false true false hello world, how are you ? THE QUICK BROWN FOR JUMPS OVER THE LAZY DOG. 1000 number Deepika

Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript