Capitalize first letter of every word in a string Javascript [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Below I have written my code, apparently there is a parameter declaration issue. Can someone please point out where such issue is?
//Title case
function titleCase(str) {
str = str.toLowerCase().split(' ');
let result = str.map(function(val {
return val.replace(val.charAt(0), val.charAt(0).toUpperCase());
}) return str = result.join(" ");)
}
console.log(titleCase("I am a little tea pot")); //I Am A Little Tea Pot

function(val { is missing the closing arguments bracket.
function titleCase(str) {
str = str.toLowerCase().split(' ');
let result = str.map(function(val){
return val.replace(val.charAt(0), val.charAt(0).toUpperCase());
})
return str = result.join(" ");
}
console.log(titleCase("I am a little tea pot")); //I Am A Little Tea Pot

Related

Why doesn't variable update in for loop JavaScript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am doing a coding challenge where the letters in a string are replaced by their partner letters, I know that there is probably a better way to do this, but I just want to know why this doesn't work. Here is the code:
function DNAStrand(dna){
var strObject = {"A":"T", "T":"A", "C":"G", "G":"C"};
let newDna = "";
for (let i=0; i < dna.lenght; i++){
newDna += strObject[dna[i]];
}
return newDna
}
the function returns an empty string or "", the value of newDna before the loop, it doesn't change.
for (let i=0; i < dna.lenght; i++){
You made a typo. Change this to dna.length.

Javascript replace exact match string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have the following
var string = '1,7,12,15,16,29';
if I just want to replace the number 1, i will use the following
removeStr(1);
function removeStr(str1)
{
var string = '1,7,12,15,16,29';
var newstr = string.replace(str1, '');
alert('new val is ' + newstr);
}
But doing this, will end up removing the number 1 in 12,15,16.
How do I just remove the exact match 1 in this example.
Thanks
You could use boundaries (\b) in a regexp that to match a whole word only. Changed your test string to one where your question would be applicable
function removeStr(str1)
{
var string = '11,71,12,1,16,21';
var newstr = string.replace(new RegExp("\\b"+str1+"\\b"), "");
console.log('new val is ' + newstr);
}
removeStr("1");
function replaceOne(str1, str2){
var arr = str2.split(",");
var newStr = "";
for(var i=0; i<arr.length; i++){
if(arr[i]!=str1){
newStr = (newStr=="")?arr[i]:newStr+","+arr[i];
}
}
console.log(newStr);
}
You are trying to do it on strings.
you might consider to make it an array
var string = '1,7,12,15,16,29';
var arr=string.split(",");
var newArr=arr.splice("1");
string=newArr.join(",");
console.log(string);
Hope this helps

Attempting to use a 'for' loop to build an Array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am attempting to build an array whose entries are the letters of any given word. I though I had written a clever bit of code, but it doesn't work in the slightest! And feedback or help would be greatly appreciated:
var inputWord = prompt("PALINDROME CHECKER:");
var numberOfLetters = inputWord.length;
var letters = [];
for(i=0; i++; i<numberOfLetters){
letters[i] = inputWord.substring(i,i+1);
};
Thanks,
CPR
If you want an array with the letters of a string, just split the string with no pattern:
var string = "My string is this";
var array = string.split("");
console.log(array);
Your for loop is wrong. Try:
var inputWord = prompt("PALINDROME CHECKER:");
var numberOfLetters = inputWord.length;
var letters = [];
for(i=0; i<numberOfLetters; i++){
letters[i] = inputWord.substring(i,i+1);
};
The order of the for loop parameters should be iterator, then condition, then action - basically, "for my variable i, if i is less than the number of letters, then increment i"

Return the first "n" count of characters of a string ( Javascript ) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I've used this :
String.Prototype.left = function left(count){
return this.substr(0,count);
}
And apply it this way :
var string = "Hello Stack!";
console.log(string.left(5));
And the console tells me :
TypeError: example.left is not a function
console.log(example.left(5));
How can I fix it? And where is the problem?
String.prototype.left = function(index){
return this.substring(0,index);
}
var str = "Hello World";
console.log(str.left(5));

JavaScript match() function does not give output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.

Categories