enter image description here
How to make observe the Capital word behavior ?
source code :
enter code here
document.getElementById("btnReverse").onclick = function () {
reverse();
};
String.prototype.replaceAt = function (index, replacement) {
if (index >= this.length) {
return this.valueOf();
}
return this.substring(0, index) replacement this.substring(index 1);
};
function reverseWord(word) {
let wordLength = word.length;
let reversedWord = word;
for (let j = 0; j < wordLength / 2; j ) {
let temp = reversedWord[j];
reversedWord = reversedWord.replaceAt(j, word[wordLength - 1 - j]);
reversedWord = reversedWord.replaceAt(wordLength - 1 - j, temp);
}
return reversedWord;
}
function reverse() {
let text = document.getElementById("text").value;
let splittedText = text.split(" ");
let reversedWords = [];
for (let i = 0; i < splittedText.length; i ) {
let word = splittedText[i];
let reversedWord = reverseWord(word);
reversedWords.push(reversedWord);
}
document.getElementById("demo").innerHTML = reversedWords.join(" ");
}`enter code here`
CodePudding user response:
Change the below line of reverse() method and use textContent
instead of value.
let text = document.getElementById("text").textContent;
document.getElementById("btnReverse").onclick = function () {
reverse();
};
String.prototype.replaceAt = function (index, replacement) {
if (index >= this.length) {
return this.valueOf();
}
return this.substring(0, index) replacement this.substring(index 1);
};
function reverseWord(word) {
let wordLength = word.length;
let reversedWord = word;
for (let j = 0; j < wordLength / 2; j ) {
let temp = reversedWord[j];
reversedWord = reversedWord.replaceAt(j, word[wordLength - 1 - j]);
reversedWord = reversedWord.replaceAt(wordLength - 1 - j, temp);
}
return reversedWord;
}
function reverse() {
let text = document.getElementById("text").textContent;
let splittedText = text.split(" ");
let reversedWords = [];
for (let i = 0; i < splittedText.length; i ) {
let word = splittedText[i];
let reversedWord = reverseWord(word);
reversedWords.push(reversedWord);
}
document.getElementById("demo").innerHTML = reversedWords.join(" ");
}
<html>
<head></head>
<body>
<div>
<button id="btnReverse">Reverse</button>
<div id="text">I am a great human</div>
<div id="demo"></div>
</div>
</body>
</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
This function re-capitalize a reversed word:
function reCapitalize(word) {
// Check if last letter is an uppercase
// If so, return the word with first letter converted to capital
if (word[word.length - 1] === word[word.length - 1].toUpperCase())
return word[0].toUpperCase() word.toLowerCase().substring(1, word.length);
// return word unchanged
return word;
}
So your reverse function will be like this:
function reverseWord(word) {
let wordLength = word.length;
let reversedWord = word;
for (let j = 0; j < wordLength / 2; j ) {
let temp = reversedWord[j];
reversedWord = reversedWord.replaceAt(j, word[wordLength - 1 - j]);
reversedWord = reversedWord.replaceAt(wordLength - 1 - j, temp);
}
return reCapitalize(reversedWord);
}