This question already has answers here:
How to count duplicate value in an array in javascript
(35 answers)
Closed 2 years ago.
How can I count how many times wordToCount are in the phrasesToCheck. And how to put this number to counter variable ?
let counter = [];
let wordToCount = ["tomato","cat"];
let phrasesToCheck = ['my cat like potatoes','cat like apple','my golden fish like tomato'];
counter[0] = 1; //tomato
counter[1] = 2; //cat
let wordToCount = ["tomato","cat"];
let phrasesToCheck = ['my cat like potatoes','my cat like apple','my golden fish like tomato'];
let allwords = phrasesToCheck.join(' ').split(' ');
let counter = wordToCount.map(word => allwords.reduce((acc, v) => acc += v === word, 0));
console.log(counter);
Related
This question already has answers here:
Getting a random value from a JavaScript array
(28 answers)
Closed 4 months ago.
Make sentences from the three arrays. Firt index with first index form each array so on..
10 sentences. (This I have figured out and managed)
Use Math.random to mix the words from the arrays randomly to make new sentences, doesnt matter if a word get used two times.
Question: where do i best put the Math.random in my code?
let substantiv = ['Daddy', 'Jag', 'Hästen', 'Mamma', 'Glaset', 'Gameboy', 'Pelle','Blondie', 'Sängen', 'Bilen']
let verb = ['cyklar', 'rider', 'bakar', 'springer', 'hoppar', 'äter', 'dricker', 'går', 'läser', 'sover']
let adj = ['bra', 'dåligt', 'roligt', 'inte', 'alltid', 'på Söndag', 'aldrig', 'imorgon', 'idag', 'snabbt']
let cont = document.createElement('div')
document.body.append(cont)
for(let i=0; i<10; i++){
const el1 = document.createElement('div')
el1.textContent = `${substantiv[i]} `+`${verb[i]} `+`${adj[i]}`
cont.append(el1)
}
I found this solution;
for(let i=0; i<10; i++){
const random1 = Math.floor(Math.random()* substantiv.length)
const random2 = Math.floor(Math.random()* verb.length)
const random3 = Math.floor(Math.random()* adj.length)
const el2 = document.createElement('div')
el2.textContent = `${substantiv[random1]} ${verb[random2]} ${adj[random3]}`
cont.append(el2)
}
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
First let me say am a novice at Javascript so this may be more obvious than I think. But what I'm trying to do is create a new Javascript array from two existing arrays. My thinking is to create a for loop, test if they are equal and if not push that number into a new array. For example.
var allArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var myArr = [7,8,9,10,14,17];
var fnlArr = [];
What I would like to end up with is fnlArr = [1,2,3,4,5,6,11,12,13,15,16,18,19,20];
Something like:
for (n = 0; n < myArr.length; n++){
for (p = 0; p < allArr.length; p++){
if (p!==myArr[n]){
fnlArr.push(p);}
}
}
I was also thinking maybe to grep to see if they are the same???
Any help would be appreciated.
You can use .filter:
const allArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
const myArr = [7,8,9,10,14,17];
const set = new Set(myArr);
const fnlArr = allArr.filter(e => !set.has(e));
console.log(fnlArr);
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"
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
I want to optimize my javascript code with a FOR by i can't do this and i don't know why.
My code :
let pokemon1 = 'premier';
let pokemon2 = 'second';
let pokemon3 = 'troisieme';
for (var i = 1; i < 4; i++) {
console.log(pokemon[i]);
}
Do you know why it doesn't work ?
Thank you very much and sorry if i am a noob.
You should place the pokemon in an array:
let pokemon = [];
pokemon[0] = "premier";
pokemon[1] = "second";
pokemon[2] = "troisieme";
for(var i = 0; i < pokemon.length; i++){
console.log(pokemon[i])
};
Followed by some reading time: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
Since you are using a list, you should use [] to define an array :
let pokemons = ['premier', 'second', 'troisième'];
for (let i = 0; i < pokemons.length; i++) {
console.log(pokemons[i]);
}
See https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array for more information.
Also you should note that the first element of a list is 0.
So basically pokemons[0] === 'premier and pokemons[2] === 'troisième'
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();