This question already has answers here:
Convert JavaScript String to be all lowercase
(15 answers)
Closed 3 years ago.
I have the following code:
var str = "abcabcABCABC"
var chars = str.split("");
var lettersCount = {};
for (var i = 0; i < chars.length;i++)
{
if (lettersCount[chars[i]] == undefined )
lettersCount[chars[i]] = 0;
lettersCount[chars[i]] ++;
}
for (var i in lettersCount)
{
console.log(i + ' = ' + lettersCount[i]);
}
This code is counting how many same letters are in a word. What am I trying is to convert the uppercase letters to lowercase so it should show like this: a - 4, b -4, now it shows: a - 2, A - 2.
I've just started with Js so please be good with me. :)
If you just need the string to be converted into lowercase letter then you can do it like this:-
var str = "abcabcABCABC";
var newStr = str.toLowerCase();
console.log(newStr);
Hope this helps.
Related
This question already has answers here:
How do I split a string into an array of characters? [duplicate]
(8 answers)
How to get character array from a string?
(14 answers)
Closed 1 year ago.
how to print as follows in javascript ?
if my input is "smart"
then my output could be "s,m,a,r,t"
I've tried by using this logic but i got the output as
s,m,a,r,t,
let a = " ";
str = userInput[0];
console.log(str);
for (let i = 0; i < str.length; i++) {
a = a + str[i] + ",";
}
console.log(a.trim());
You can use the split method in js
var str = "smart";
var res = str.split("").join();
console.log(res)
//if you want it to remain an array don't do the join
This question already has answers here:
Capitalize the first letter of every word
(9 answers)
Closed 3 years ago.
I'm writing a bot for discord and using this project to teach myself javascript. I have a predefined string set to message variable and I want this to script to change the first letter of each word in the string to a capital, but so far the function is only returning the message as it was spelt. I cannot understand why
var string = message.substr(message.indexOf(" ")+1);
function capital_letter(str)
{
str=str.split(" ");
for (var i = 0, x = str.length; i<x, i++;)
{
str[i] = str[i].charAt(0).toUpperCase() + str[i].substr(1);
};
return str.join(" ");};
If message = "ring of life" I would expect the output to be "Ring Of Life"
You had some syntax errors, here's a corrected version of your captial_letter function:
function capital_letter (str) {
str = str.split(' ')
for (var i = 0; i < str.length; i++) {
const firstChar = str[i].charAt(0)
str[i] = firstChar.toUpperCase() + str[i].substr(1)
};
return str.join(' ')
};
The biggest one was to separate your loop parameters using ; instead of ,:
for (var i = 0; i < str.length; i++)
p.s. looks like you might benefit from a better IDE :-)
you can try this.
str.toLowerCase().replace(/\b\w{3,}/g, function (l) {
return l.charAt(0).toUpperCase() + l.slice(1);
});
This question already has answers here:
jQuery / Get numbers from a string
(8 answers)
Closed 5 years ago.
I have strings like
'156p1m2s10c'
'15p13m22s3c'
'1644p31m142s3c'
out of which I want to get all numerics separately.
An array will also work like:
156
--
1
--
2
--
33
How to do it using Jquery. I tried code mentioned below, but it's all going wrong.
function myFunction() {
var str = "156p1m2s33c";
var prodId = cust_code.substring(0, url.indexOf('p'));
var metalId = cust_code.substring(url.indexOf('p') + 1, url.indexOf('m'));
var prodId = cust_code.substring(url.indexOf('m') + 1, url.indexOf('s'));
}
What is wrong in the code?
You can use String#match with a RegExp. You can use Array#map afterwards to convert the strings to numbers if needed.
var str = '156p1m2s33c';
var numbers = str.match(/\d+/g);
console.log(numbers);
What's wrong in your code:
cust_code and url are not part of the function. Refer to str.
You declare prodId twice.
You don't handle the number between "s" and "c".
You are doing a lot of duplicated search (index of p, index of m, etc...).
Your function doesn't return anything, or do anything with the results.
Using regular expressions is more fitting for this case.
function myFunction() {
var str = "156p1m2s33c";
var pI = str.indexOf('p');
var mI = str.indexOf('m');
var sI = str.indexOf('s');
var cI = str.indexOf('c');
var prodId = str.substring(0, pI);
var metalId = str.substring(pI + 1, mI);
var anotherId1 = str.substring(mI + 1, sI);
var anotherId2 = str.substring(sI + 1, cI);
return [prodId, metalId, anotherId1, anotherId2];
}
console.log(myFunction());
This question already has answers here:
Split string on the first white space occurrence
(16 answers)
Closed 5 years ago.
I want to split string in array based just on first white space in string.
Like this:
var name = "Jone Doe Doone";
var res = ["Jone", "Doe Doone"];
Here is an example where I have used indexOf() to find the first space and then return with a splitted array with substring()
function splitAtFirstSpace(str) {
if (! str) return [];
var i = str.indexOf(' ');
if (i > 0) {
return [str.substring(0, i), str.substring(i + 1)];
}
else return [str];
}
console.log(splitAtFirstSpace("Jone Doe Doone"));
console.log(splitAtFirstSpace("BooDoone"));
console.log(splitAtFirstSpace("Doe Doone"));
Simple.
function splitFirst(s) {
var firstW = s.indexOf(" ");
if (firstW < 0) {
return s;
}
var array = [];
array.push(s.substring(0, firstW));
array.push(s.substring(firstW, s.length - 1));
return array;
}
Note: Javascript coding convention says variables have to start with lowercase.
Try the following answer.
var Name = "Jone Doe Doone";
var result = [Name.split(" ")[0]];
result.push(Name.substr(Name.split(" ")[0].length).trim());
console.log(result);
This question already has answers here:
Is there any pre-built method for finding all permutations of a given string in JavaScript?
(8 answers)
Closed 8 years ago.
So If a have A, B, C ,
I want to create some strings whith length 4
so the output will be
AAAA
AAAB
AAAC
AABA
AABB
AABC
ABAB
....
CCCC
There are some comments, so you can understand this. Thanks to http://www.walmik.com/2013/03/rearrange-letters-of-a-word/
function permutate(theWord){
//Array to store the generated words
var words = [];
/**
* Recursive function to split a string and rearrange
* it's characters and then join the results
*/
function rearrange(str, prefix) {
var i, singleChar, balanceStr, word;
//The first time round, prefix will be empty
prefix = prefix || '';
//Loop over the str to separate each single character
for(i = 0; i < str.length; i++) {
singleChar = str[i];
balanceStr = str.slice(0, i) + str.slice(i+1);
//join the prefix with each of the combinations
word = prefix + singleChar + balanceStr;
//Inject this word only if it does not exist
if(words.indexOf(word) < 0) words.push(word);
//Recursively call this function in case there are balance characters
if(balanceStr.length > 1) rearrange(balanceStr, prefix + singleChar);
}
}
//kick start recursion
rearrange(theWord);
return words;
}
var permutatedWord = permutate('goal');
console.log(permutatedWords);