Make a camel-case function using a for loop in Javascript - javascript

I am brand new to Javascript and trying to figure out how to camel-case any string by using a for loop. This is what I have so far.
function camelCase(str) {
var splitStr = "";
var result = "";
splitStr = str.split(" ");
for(var i = 0; i < splitStr.length; i++){
result += splitStr[i][0].toUpperCase() +
splitStr[i].slice(1);
}
return result;
}
console.log(camelCase("hello there people"));
it returns "HelloTherePeople" - How do you I make the very first index of splitStr (splitStr[0][0]) be excluded from toUpperCase, but still included in the beginning of the string?

What is your delimiter? This method assumes an underscore _. Change it to a space if you want a space. Or make it a variable you can pass to camelize.
if( !String.prototype.camelize )
String.prototype.camelize = function(){
return this.replace(/_+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
}
"a_new_string".camelize()
//"aNewString"
The regex /_+(.)?/g/ says find 1 or more _ characters followed by any character ., the (.) creates a capturing group, so you are able to get that one character. It's passed to the function as the second paramater chr. The ? means not greedy so it will stop at the next _. The g means globally, pretty much means find all matches.
String.prototype.replace reference

change return like this:
return result[0].toLowerCase()+result.substr(1);

You can do a check inside the loop to see if you are on the first index.
function camelCase(str) {
//splitStr will be an array
var splitStr = [];
var result = "";
splitStr = str.split(" ");
//Capitalize first letter of words starting from the second one
for(var i = 0; i < splitStr.length; i++){
//first word
if (i===0) {
//Good practice to lowercase the first letter regardless of input
result += splitStr[i][0].toLowerCase() + splitStr[i].slice(1);
}
else {
//rest can proceed as before
result += splitStr[i][0].toUpperCase() +
splitStr[i].slice(1);
}
}
return result;
}
console.log(camelCase("hello there people"));
Alternatively, the loop can even start on the second index. However, you will have to check if the length of splitStr before running a loop from the second index

Related

How to remove the (.) in a string and then display every alphabetic character with a dot

I would like to have a a function who takes a string a parameter, removes the dots, loops trough every alphabetic character and display like this (A.B.C.) when input is (A..B..C) for example.
How can I build this function?
Here for I have the next function in mind, unfortunately is not working I get a output result like this (hfff) when input string is "h..f.ff", would like to have this output (H.F.F.F)
function filter (initials) {
let result = initials.replace(/\./g, '')
let i;
for (i = 0; i < result.length; i++) {
result[i] + ".";
}
return result
console.log(result)
}
const initials = "h..f.ff"
console.log(filter(initials))
You could use split, map and join
function filter(initials) {
return initials.replace(/[^a-z]/gi, '') // Remove non-letters
.toUpperCase()
.split('') // Convert to an Array
.map(l => l + '.') // Add dots
.join(''); // Join
}
const initials = "h..f.ff";
console.log(filter(initials));
You need to assign this
result[i] + ".";
to something. Do:
let i,newresult;
for (i = 0; i < result.length; i++) {
newresult += result[i] + ".";
}
well you can:
make the string uppercase
split the string char-by-char
filter only letters
join using "."
function format(string){
return string.toUpperCase()
.split("")
.filter(c => /[a-zA-Z]/.test(c))
.join(".")
}
format("h..f.ff") // "H.F.F.F"
Use replaceAll to remove all . char with nothing.
Then from that string, make all letters uppercase.
From that string, split the whole word up into and array of letters using split (Each piece of the string on each side of the parameter passed to split gets turned into an element in a new array. if you leave the parameter blank, it's just each character in the string)
Finally join each those elements together with a . between them using join
function filter (initials) {
initials=initials.replaceAll('.','');
initials=initials.toUpperCase();
initials=initials.split('');
initials=initials.join('.');
return initials;
}
var test = "h..f..t....e.Z";
console.log(filter(test));
Thanks #blex for the snippet advice

Replace content present in the nested brackets

Input = ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))
Expected Output = ABCDEF,GHIJKLMN,OPQRSTUVW
Tried so far
Output = Input.replace(/ *\([^)]*\)*/g, "");
Using a regex here probably won't work, or scale, because you expect nested parentheses in your input string. Regex works well when there is a known and fixed structure to the input. Instead, I would recommend that you approach this using a parser. In the code below, I iterate over the input string, one character at at time, and I use a counter to keep track of how many open parentheses there are. If we are inside a parenthesis term, then we don't record those characters. I also have one simple replacement at the end to remove whitespace, which is an additional step which your output implies, but you never explicitly mentioned.
var pCount = 0;
var Input = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
var Output = "";
for (var i=0; i < Input.length; i++) {
if (Input[i] === '(') {
pCount++;
}
else if (Input[i] === ')') {
pCount--;
}
else if (pCount == 0) {
Output += Input[i];
}
}
Output = Output.replace(/ /g,'');
console.log(Output);
If you need to remove nested parentheses, you may use a trick from Remove Nested Patterns with One Line of JavaScript.
var Input = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
var Output = Input;
while (Output != (Output = Output.replace(/\s*\([^()]*\)/g, "")));
console.log(Output);
Or, you could use a recursive function:
function remove_nested_parens(s) {
let new_s = s.replace(/\s*\([^()]*\)/g, "");
return new_s == s ? s : remove_nested_parens(new_s);
}
console.log(remove_nested_parens("ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))"));
Here, \s*\([^()]*\) matches 0+ whitespaces, (, 0+ chars other than ( and ) and then a ), and the replace operation is repeated until the string does not change.

Changing individual characters in a string does not work in a loop [duplicate]

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Closed 4 years ago.
So, I'm working on some exercise questions. My code seems to be working fine up until I decide to loop through the string to replace any instance of a period with nothing. For some reason, the loop doesn't work. I imagine it has something to do with not calling it somehow, but I'm not sure how to call the loop. I thought that loops automatically overwrote what they are looping through. Here is the exercise and my incomplete solution:
Write a JavaScript function to parameterize a string.
function string_parameterize(string) {
var lowercase_string = string.toLowerCase();
var split_string = lowercase_string.split(" ");
var joined_string = split_string.join("-");
for (i = 0; i < joined_string.length; i++) {
if (joined_string[i] === ".") {
joined_string[i] === "";
}
}
return joined_string;
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));
The answer should be robin-singh-from-usa without the period, but it keeps coming out as robin-singh-from-usa. with the period.
The other answers are not taking into account that strings in JavaScript are immutable. You can not change individual characters in a string. You build a new string.
In JavaScript, strings are immutable. Trying to change the characters in a string does not work:
function string_parameterize(string) {
var lowercase_string = string.toLowerCase();
var split_string = lowercase_string.split(" ");
var joined_string = split_string.join("-");
for (i = 0; i < joined_string.length; i++) {
if (joined_string[i] === ".") {
joined_string[i] = "";
}
}
return joined_string;
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA.")); //This will not work:
You can build a new string using your for loop to individually add each character that is not a . to the newString:
function string_parameterize(string) {
var lowercase_string = string.toLowerCase();
var split_string = lowercase_string.split(" ");
var joined_string = split_string.join("-");
var newString = '';
for (i = 0; i < joined_string.length; i++) {
if (joined_string[i] !== ".") {
newString += joined_string[i];
} //We are replacing '.' with nothing, '', so no need for an else
}
return newString;
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));
Regular Expressions
This would, however, normally be done with Regular Expressions, specifically the .replace() method:
function string_parameterize(string) {
var lowercase_string = string.toLowerCase();
var newString = lowercase_string.replace(/ /g,'-'); //Replace all spaces with '-'.
var newString = newString.replace(/\./g,''); //Replace all '.' with nothing. The '.'
// has to be quoted with \. as it
// has special meaning in a RegExp.
return newString;
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));
Which can be done all in one statement:
function string_parameterize(string) {
return string.toLowerCase().replace(/ /g,'-').replace(/\./g,'');
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));
You can put in a [...]+ every character you don't want have in the output.
var res = string.toLowerCase().replace(/[%\(\)\.\s]+/g, "-").replace(/-$/, "");
// ^ ^ ^ ^
// Here the characters you don't want to have in the output
+ means matched one ore more times. Replace the matched characters with -.
Then remove last - with -$.
In total
function string_parameterize(string) {
var res = string.toLowerCase().replace(/[%\(\)\.\s]+/g, "-").replace(/-$/, "");
return res;
}
console.log(string_parameterize("Это тест")); // A russian sentence
console.log(string_parameterize("Robin Singh%%() from USA. "));
console.log(string_parameterize("Robin ...Singh from USA....."));
console.log(string_parameterize("Robin Singh from USA "));
console.log(string_parameterize("Robin Singh from USA"));
Info about regular expression.
function string_parameterize(string) {
var lowercase_string = string.toLowerCase();
var split_string = lowercase_string.split(" ");
var joined_string = split_string.join("-");
joined_string = joined_string.replace(".", "");
return joined_string;
}
//Test Data :
console.log(string_parameterize("Robin Singh from USA."));
This is working in IE.
function string_parameterize(str){
return str.toLowerCase().replace(".", "").split(" ").join("-");
}
console.log(string_parameterize("Robin Singh from USA."));
//will result in: robin-singh-from-usa
Your code is not working because in if condition you are checking for joined_string[i]==="." to be equal to '.' and actually it is equal to 'USA.'. That is why this if condition never met with and return the '.' in the final result:
if (joined_string[i]===".") {
joined_string[i]==="";
}
You're checking the value of joined_string[i], rather than assigning it.
joined_string[i] = "";
not
joined_string[i] === "";
Change:
if (joined_string[i]===".") {
joined_string[i]==="";
}
to
if (joined_string[i]===".") {
joined_string[i]="";
}

Please explain to me prototypes in javascript using the code below

I am trying to use the prototype method of writing functions that can be implemented by strings to capitalise every first letter of every word. I would like to call this function like,
var str = "Example of a string";
str.toJadenCase();
This is the function I am trying to write:
String.prototype.toJadenCase = function () {
//split the statement into each word
if (String.prototype.length !== 0)
{
var eachWord = String.prototype.split(" ");
var n = eachWord.length;
if(n !== 0)
{
//loop through the array of words
for(var i = 0; i < eachWord.length; i++){
//for each loop, split the word into individual characters
var charArray = eachWord[i].split("");
//capitalise the first element of character array
charArray[0] = charArray[0].toUpperCase();
//join all elements in character array to string
eachWord[i] = charArray.join("");
}
//join all the words to form the statement
String.prototype = eachWord.join(" ");
return String.prototype;
}
}
};
I had written it this way before:
var capitaliseInitial = function(sampleText){
var textString = sampleText;
//split the statement into each word
var eachWord = textString.split(" ");
//loop through the array of words
for(var i = 0; i < eachWord.length; i++){
//for each loop, split the word into individual characters
var charArray = eachWord[i].split("");
//capitalise the first element of character array
charArray[0] = charArray[0].toUpperCase();
//join all elements in character array to string
eachWord[i] = charArray.join("");
}
//join all the words to form the statement
textString = eachWord.join(" ");
return textString;
}
I would like to call this function like,
var str = "Example of a string";
str.toJadenCase();
You can't, strings are immutable. You would have to call it like this:
str = str.toJadenCase();
In your function, you're using String.prototype incorrectly. String.prototype is the object containing the various String-specific methods. It's assigned as the underlying prototype of all strings.
Where you're using String.prototype, you should be using this, and instead of trying to assign to it (this = ... is invalid), return the result.
The simple way to do what you're doing is to:
Split the string into an array of words, as you have
Loop through that array either building up a new string with the capitalized words via +=, or building a new array with the capitalized words and then doing Array#join at the end to put it back together.
Return the string you built
Something like this:
String.prototype.toJadenCase = function() {
var result = this;
//split the statement into each word
if (this.length !== 0) {
result = this.split(" ").map(function(word) {
return word.substring(0, 1).toUpperCase() + word.substring(1);
}).join(" ");
}
return result;
};
snippet.log("this is a test".toJadenCase());
snippet.log("oneword".toJadenCase());
snippet.log("blank: " + ("".toJadenCase()));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Note I've done away with the check if the array of words' length isn't 0: It can't be 0 if you've pre-checked the length as you have.
use RegExp and php like naming
str.ucwords()
String.prototype.ucwords = function() {
return this.replace(/\b\S/g,function(c){
return c.toUpperCase()
}
}
Here's how I did mine.
Split the string into an array of words, as you have
Loop through that array either building up a new string with the capitalized words via +=, or building a new array with the capitalized words and then doing Array#join at the end to put it back together.
Return the string you built
String.prototype.toJadenCase = function () { return this.split(" ").map(function(word){ return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); }
This look like one of the Code Wars Katas - this was my solution -
String.prototype.toJadenCase = function () {
// assign 'this' keyword to a variable and split String into an array
var result = this.split(" ");
/* loop through the array changing first character of each item to
uppercase & adding it to the remaining letters in each item */
for(i = 0; i < result.length; i++) {
result[i] = result[i].charAt(0).toUpperCase() + result[i].substring(1);
}
//finally return items joined back together in a string
return result.join(' ');
};
another way to do this would be like:
String.prototype.toJadenCase = function() {
return this
.split(" ")
.map(i => i.replace(i[0], i[0].toUpperCase()))
.join(" ");
};

Javascript Array Issues

The task at hand;
Return true if the string in the first element of the array contains
all of the letters of the string in the second element of the array.
For example, ['hello', 'Hello'], should return true because all of the
letters in the second string are present in the first, ignoring case.
The arguments ['hello', 'hey'] should return false because the string
'hello' does not contain a 'y'.
Lastly, ['Alien', 'line'], should return true because all of the
letters in 'line' are present in 'Alien'.
My attempted solution that isn't working;
function mutation(arr) {
if (arr[0].toLowerCase().indexOf(arr[1].toLowerCase()) >= 0){
return true;
}else{
return false;
}
return arr;
}
mutation(['hello', 'hey']);
Can someone please explain to me why it isn't working. I am not looking for an answer just an explanation so that I can understand better.
Many thanks
Try this:
$('#id').click(function() {
var arr = ['hello','hey'];
var isExist = false;
for (i = 0;i < arr[1].length; i++) {
if(arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) >= 0)
isExist = true;
else {
isExist = false;
break;
}
}
if (isExist)
alert('true'); // In case of all chars of 2nd present in 1st arr
});
what you are doing is that, you are converting first element to lower case and than trying to find the second element in first array.
your solution will work for the cases where the whole second element exists in first element as a sub string or both elements are exactly same.
for example it'll work for following examples
['hello', 'Hello']
['hello', 'ell']
['hello', 'He']
But it will not work when letters in second string are shuffled first string. for following examples your solution will fail.
['Alien', 'line']
['hello', 'elo']
['hello', 'Hol']
If whole second word does not exists as it is in first word your solution will not work.
Let me know if this is helpful. i'll provide you the solution if you need.
As another answer correctly states, your code does not work because it's looking in the first string for the second string, in its entirety, with the letters in the same order. Instead, you need to look at each letter in the second string individually and check that it's in the first string.
We'll adopt the strategy of "Programming in English and going backward". The English for the overall problem is:
Does every letter to check match?
We can write this in JavaScript as
letters_to_check . every(matches)
We can calculate letters_to_check as input[1].toLowerCase().split('').
Now we have to write the matches function, which can be expressed in English as
Is the letter found in the string to check against?
So we write
function matches(letter) { return string_to_heck_against.search(letter); }
string_to_check_against is of course input[0].
The entire program is thus:
function mutation(input) {
// Check if a letter is in the string to be checked against.
function matches(letter) { return string_to_check_against.search(letter); }
var string_to_check_against = input[0].toLowerCase();
var string_to_check = input[1].toLowerCase();
var letters_to_check = string_to_check.split('');
return letters_to_check . every(matches);
}
If you are programming in ES6, this could be written as
function mutation([source, target]) {
return target .
toLowerCase() .
split('') .
every(letter => source.toLowerCase().search(letter));
}
According to your output you have to check all the characters of array second string with array first string. For this you have to check all characters are available or not in the first string, not using the direct string. If we use strings directly, it will look for total string not for the characters.
Try the following:
var arr = ['Alien', 'line'];
var arr1 = ['hello','hey'];
var arr2 = ['hello','Hello'];
function mutation(arr) {
var str1 = arr[0].toLowerCase();
var str2 = arr[1].toLowerCase();
var o = false;
for (var i=0;i<str2.length;i++) {
o = (str1.indexOf(str2[i]) !== -1);
if (!o) break;
}
return o;
}
console.log('first arr : ', mutation(arr));
console.log('second arr :', mutation(arr1));
console.log('third arr :', mutation(arr2));
This is my working code. You can check. The idea is to transfer both strings in the array to lowercase. Then we compare both strings together.
I take every single letter from the shorter string, then use the indexOf function to check if it's in the longer string. Every time there is a match, the valule no1 or no2 will increase.
If the value of no1 or no2 equal with the length of the shorter string, it means all the letters in the shorter string is inside the longer string.
function mutation(arr) {
// transfer both strings in the array to lower case
var val1 = arr[0].toLowerCase();
var val2 = arr[1].toLowerCase();
// check if val2 is shorter than val1
if(val1.length >= val2.length){
var no1 = 0;
// Check every if any letter in val2 is in val1
for(var i = 0; i < val2.length; i++){
if(val1.indexOf(val2[i]) != -1){
// plus 1 if there is a match
no1++;
}
}
// check if the value of no1 equals to length of val2
if(no1 == val2.length){
return true;
}else{
return false;
}
}else if(val2.length > val1.length){
var no2 = 0;
for(var j = 0; j < val1.length; j++){
if(val2.indexOf(val1[j]) != -1){
no2++;
}
}
if(no2 == val1.length){
return true;
}else{
return false;
}
}
}

Categories

Resources