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
Related
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
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 5 years ago.
Improve this question
I'm trying to create an object using a given string where each word has a property stating its length.
var strings = {};
function findLongestWord(str) {
var splitStr = str.split(" ");
for (var i = 0; i <= str.length; i++){
strings[splitStr[i]] = splitStr[i].length;
}
return strings;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
I end up getting:
"TypeError": Cannot read property "length" of undefined.
If I were to replace splitStr[i].length with splitStr[0].length, the code runs properly, but of course giving me the same number for each word in the object.
Any help is appreciated, thanks.
you are looping over wrong array. you should use i < splitStr.length.
var strings = {};
function findLongestWord(str) {
var splitStr = str.split(" ");
for (var i = 0; i < splitStr.length; i++){
strings[splitStr[i]] = splitStr[i].length;
}
return strings;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
function validPhone(phoneNum) {
var reg = new RegExp("^\d{3}-\d{4}$");
if (reg.test(phoneNum)) {
return "True";
}
else {
return "False";
}
}
//main code\\
<html>
<head>
<script language="javascript" type="text/javascript" src="codechallenge3.js"></script>
</head>
<body>
<script>
var phoneNum = "555-555";
document.write("Check the following phone number: ", phoneNum, " = ", validPhone(phoneNum), "<br>");
</script>
</body>
</html>
//Why does it keep returning false? I've tried to fix it , but it's not returning true.\
Your regular expression is looking for 4 digits not 3. Try 555-5555
Valid phone number would be ddd-dddd where d is a digit.
You have 2 problems:
Change
var phoneNum = "555-555";
to
var phoneNum = "555-5555";
if you want to have 4 digits in the second group.
Change
var reg = new RegExp("^\d{3}-\d{4}$");
to
var reg = new RegExp("^\\d{3}-\\d{4}$");
or to
var reg = new RegExp("^[0-9]{3}-[0-9]{4}$");
or to
var reg = /^\d{3}-\d{4}$/;
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"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I'm making a function that takes a string, cuts the first half (leaving middle character if odd string.length) and adds first half to end of string.
For some reason my function only partlyworks: it adds the substr to the end but doesn't cut it from the start. I tried .replace but not working.
What am I doing wrong? And/or is there a better way?
replace returns a new string with the replacement, it doesn't modify the string you call it on.
Additionally, as Pointy pointed out, you've passed the literal string 'substr' in, rather than passing in the variable substr.
So:
s = s.replace(substr, '');
a friend just gave another way to write a function that does what I wanted mine to do . I'm an amoeba and you're all wizards
function doit(s){
split = s.length /2;
if(split % 2 !== 0) { split = split-1; }
var partOne = s.slice(0, split);
var partTwo = s.slice(split + 1, s.length);
return partTwo + partOne;
}
alert(doit('123456789qwertyuio'));