How to convert split words into lowercase [duplicate] - javascript

This question already has answers here:
Javascript - Making Array Index toLowerCase() not working
(3 answers)
Closed 5 years ago.
How do i convert words i have split into lowercase ? I have the following code.
var title = 'The Quick brown fox';
var titleCase = [];
var Titlewords = title.split("");
for (var i = 0; i < Titlewords.length; i++) {
Titlewords[i].toLowerCase();
console.log(Titlewords[i]);
}

You can do a lot of things in JavaScript without writing loops by yourself. This is just another example.
const title = 'The Quick brown fox';
const words = title.split(' ').map(word => word.toLowerCase());
// print the array
console.log(words);
// print the single words
words.forEach(word => console.log(word));
Resources
Array.prototype.map()
Array.prototype.forEach()
Arrow functions

you just need to assign Titlewords[i].toLowerCase() back to Titlewords[i]
var title = 'The Quick brown fox';
var titleCase = [];
var Titlewords = title.split("");
for (var i = 0; i < Titlewords.length; i++) {
Titlewords[i] = Titlewords[i].toLowerCase();
console.log(Titlewords[i]);
}

you need to update the value
Titlewords[i] = Titlewords[i].toLowerCase();
var title = 'The Quick brown fox';
var titleCase = [];
var Titlewords = title.split("");
for (var i = 0; i < Titlewords.length; i++) {
Titlewords[i] = Titlewords[i].toLowerCase();
console.log(Titlewords[i]);
}

str.toLowerCase() returns a string, not sets it to lower. What you want to do is
Titlewords[i] = Titlewords[i].toLowerCase();

Related

Trying to make a function that will return longest word in a string [duplicate]

This question already has answers here:
Javascript: find longest word in a string
(33 answers)
Closed 2 years ago.
This what I have so far but it keeps resulting in "undefined" when I run it.
var sentence = 'Hello my friends';
var words = sentence.split;
var longWordLength = 0;
var longword = 'i';
function findLongWord (sentence){
for (a = 0; a < words.length; a++){
if (words[a].length > longWordLength){
longWordLength = words[a].length;
longWord = words [a];
return longWord}
}
console.log(longWord);
String.prototype.split() is a function
You could use a RegExp expression to split by One-or-more spaces / +/
Keep the logic within your function
JS is case sensitive, so take special care
You could use Array.prototype.forEach()
function findLongWord(sentence) {
const words = sentence.split(/ +/);
let longWord = '';
words.forEach(word => {
if (word.length > longWord.length) {
longWord = word;
}
});
return longWord;
}
console.log(findLongWord('Hello my friends')); // "friends"
Example using sort:
const findLongWord = (str) => str.split(/ +/).sort((a, b) => b.length - a.length)[0];
console.log(findLongWord('Hello my friends')); // "friends"

Convert letters to lowercase from an array in JS [duplicate]

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.

Javascript - Capitalizing first letter of each word in a string [duplicate]

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);
});

How to translate an input using a function in JavaScript

So the goal of this task is translate english input values into french and vice versa. The problem here is that I don't know how to split the whole input by spaces to get all the words one by one and translate them one by one. Thank you :)
function translateInput(){
for(i = 0; i < ('input').length; i++){
('input').eq(i).val(('value').eq(i).text());
}
}
var translateText = function() {
var translationType = document.getElementById('translation').value;
if (translationType === 'englishToFrench') {
console.log('translation used: English to French');
return 'code1';
}else if(translationType === 'frenchToEnglish'){
console.log('translation used: French to English');
return 'code2';
}else{
return "No valid translation selected.";
}
};
You can use the split function to split the string at its spaces into an array.
var str = YOUR_STRING;
var array = str.split(" ");
http://www.w3schools.com/jsref/jsref_split.asp
Then you can loop through the array and translate word by word.
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
alert(array[i]);
//Translate string
}
Or you can use a Regular Expression, by the way you can practice in a Regex Playground.
var myString = "Hello, my name is JavaScript";
var tokens = a.match(/\w+'?\w*/g); //Assuming you can take words like {"Bonsanto's", "Asus'"}
tokens.forEach(function(word){
console.log(word);
});

To count length of each word in sentence by using jQuery ,and send the largest word's length [duplicate]

This question already has answers here:
Find the longest word in a string?
(8 answers)
Closed 7 years ago.
function findLongestWord(str) {
var length = 0;
var j;
var newStr = str.split(" ");
for(var i = 0;i<15;i++){
var lentemp = newStr[i].length();
if( lentemp >length){
length === lentemp ;
}
}
return length ;
};
findLongestWord("The quick brown fox jumped over the lazy dog");
I want to get result as the length of the word which is largest?
I'm new to jQuery.
Can anybody help me to sort this?I'm just learning jQuery and i can't proceed further without finishing this.
You are using === this is Comparison operator but you need Assignment operator =. Use = instead of ===.
So this line length === lentemp ; should be length = lentemp ;
Also one more thing length is not function in javascript so you can't use length() remove braces and use .length.
This is complete snippet:
function findLongestWord(str) {
var length = 0;
var j;
var newStr = str.split(" ");
console.log(newStr.length);
for(var i = 0;i<newStr.length;i++){
var lentemp = newStr[i].length;
if( lentemp >length){
length = lentemp ;
}
}
return length ;
};
alert(findLongestWord("The quick brown fox jumped over the lazy dog"));
Here is a way to do it, map the array of strings to their lengths, and then use math.max.apply
function findLongestWord(str) {
var length = 0;
var j;
var newStr = str.split(" ");
var lengths = newStr.map(function(item) {
return parseInt(item.length, 10);
});
var longest = Math.max.apply(null, lengths);
return longest;
};
findLongestWord("The quick brown fox jumped over the lazy dog");

Categories