30-Days-Of-JavaScript
30-Days-Of-JavaScript copied to clipboard
Day:5 Array
I had a problem, I do not fully understand how to find the average middle country in the array. I realized that you need to decompose each country into letters and find the average word length. But how to do it I can't imagine
which question are talking about ?
Level 3: Find the middle country(ies) in the [countries array]
why would you need avarage word lenght and all that. solution-1 You can just => check if the array is odd or even const even= countries % 2 === 0 const len= countries.lenght if (even) { console.log(countries[len/2], countries[len/2 + 1]) } else { console.log(countries[len + 1/2]) }
Do tell me if you find any better solution!!!!!!
I have a problem with Exercise level1 number 14: I don't know how to filter the array to print the companies that have more than 'o'?!
solution-2 I think the correct solution would be: // ---------- Number of letters in country name ---------- // for (let i = 0; i < countries.length; i++) { countries[i] = countries[i].length } console.log(countries)
// ------ Finding the maximum, minimum and arithmetic mean ------ // function findMax(array) { return Math.max.apply(null, array); } function findMin(array) { return Math.min.apply(null, array); } console.log(findMax(countries), findMin(countries)); average = (findMax(countries) + findMin(countries)) / 2 console.log(average);
// ------ Search for the arithmetic mean ------ // let findAverage = countries.indexOf(average) console.log(findAverage);
I don't understand how to sort correctly. two indexes 45 and 182 should appear. Then you need to bring them to a string. This is what I still can't figure out how to do. Indeed, in the task it is indicated to find the middle country in the array. And not the average length of the array....
My soliution which was gitving undefined i have fixed it => const even= countries.length % 2 === 0 const len= Math.round(countries.length/2) if (even) { console.log(countries[len - 1], countries[len]) } else { console.log(countries[len]) }
solution-2 I think the correct solution would be: // ---------- Number of letters in country name ---------- // for (let i = 0; i < countries.length; i++) { countries[i] = countries[i].length } console.log(countries)
// ------ Finding the maximum, minimum and arithmetic mean ------ // function findMax(array) { return Math.max.apply(null, array); } function findMin(array) { return Math.min.apply(null, array); } console.log(findMax(countries), findMin(countries)); average = (findMax(countries) + findMin(countries)) / 2 console.log(average);
// ------ Search for the arithmetic mean ------ // let findAverage = countries.indexOf(average) console.log(findAverage);
I don't understand how to sort correctly. two indexes 45 and 182 should appear. Then you need to bring them to a string. This is what I still can't figure out how to do. Indeed, in the task it is indicated to find the middle country in the array. And not the average length of the array....
and in this one You can round up findAverage using Math.round() of Math.floor() but it will stop at first index .
Regarding your method i'm not sure if you are trying to find the arithmatic min
Still I think this will give you the index of middle element = > let totalEle = null countries.forEach(element => { totalEle =+ element }); let mean = Math.round((totalEle/countries.length) * 10) let middleEle = countries[mean] console.log(Math.round(mean)); console.log(middleEle);
I have a problem with Exercise level1 number 14: I don't know how to filter the array to print the companies that have more than 'o'?! if you don't get the condition you can also use =>
element.includes('oo')
solution =>
itCompanies.forEach(element => {
console.log(element);
console.log(!/(.).\1/.test(element));
if (/(o).\1/.test(element)) {
console.log(${element} contains more than one 'O'
);
} else {
console.log(${element} doesn't contain more than one 'O'
);
}
});
I have a problem with Exercise level1 number 14: I don't know how to filter the array to print the companies that have more than 'o'?!
I had the same issue, thanks for asking.
I have a problem with Exercise level1 number 14: I don't know how to filter the array to print the companies that have more than 'o'?! if you don't get the condition you can also use =>
element.includes('oo')
solution =>itCompanies.forEach(element => { console.log(element); console.log(!/(.).\1/.test(element)); if (/(o).\1/.test(element)) { console.log(
${element} contains more than one 'O'
); } else { console.log(${element} doesn't contain more than one 'O'
); } });
can you pls explain the code Tejas?
I have a problem with Exercise level1 number 14: I don't know how to filter the array to print the companies that have more than 'o'?! if you don't get the condition you can also use =>
element.includes('oo')
solution =>itCompanies.forEach(element => { console.log(element); console.log(!/(.).\1/.test(element)); if (/(o).\1/.test(element)) { console.log(
${element} contains more than one 'O'
); } else { console.log(${element} doesn't contain more than one 'O'
); } });can you pls explain the code Tejas?
This is a regex expression and it checks whether there is more than or 2 'o' in each element you can also use element.includes('oo') this condition will work too. It will check element of thes includes two 'o's.
My soliution which was gitving undefined i have fixed it => const even= countries.length % 2 === 0 const len= Math.round(countries.length/2) if (even) { console.log(countries[len - 1], countries[len]) } else { console.log(countries[len]) }
IMO this one's accurate:
console.log(countries.length) const even= countries.length % 2 === 0 const len= Math.round(countries.length/2) if (even) { console.log( countries[len]) } else { console.log(countries[len - 1]) }
itCompanies.forEach(element => { console.log(element); console.log(!/(.).\1/.test(element)); if (/(o).\1/.test(element)) { console.log(
${element} contains more than one 'O'
); } else { console.log(${element} doesn't contain more than one 'O'
); } });
Im getting this output:
HTML true HTML doesn't contain more than one 'O' CSS true CSS doesn't contain more than one 'O' JavaScript false JavaScript doesn't contain more than one 'O' React true React doesn't contain more than one 'O' Redux true Redux doesn't contain more than one 'O'
true MongoDB true MongoDB doesn't contain more than one 'O'
Using this arr=
const webTechs = [ 'HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node', 'MongoDB' ]
itCompanies.forEach(element => { console.log(element); console.log(!/(.).\1/.test(element)); if (/(o).\1/.test(element)) { console.log(
${element} contains more than one 'O'
); } else { console.log(${element} doesn't contain more than one 'O'
); } });Im getting this output:
HTML true HTML doesn't contain more than one 'O' CSS true CSS doesn't contain more than one 'O' JavaScript false JavaScript doesn't contain more than one 'O' React true React doesn't contain more than one 'O' Redux true Redux doesn't contain more than one 'O'
true MongoDB true MongoDB doesn't contain more than one 'O'
Using this arr=
const webTechs = [ 'HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node', 'MongoDB' ]
Aah seems you have found a bug in my code as it will only count two consecutive 'o' but if there are two 'o' and they have any alphabet between them it won't check.I'll have to check it again.
alright, pls post the solution if you solve it
any who can help me with this solution
First remove all the punctuations and change the string to array and count the number of words in the array
let text = 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' console.log(words) console.log(words.length) ["I", "love", "teaching", "and", "empowering", "people", "I", "teach", "HTML", "CSS", "JS", "React", "Python"]
13
2.3: Reversing an array. 1 def reverse(A): 2 N = len(A) 3 for i in xrange(N // 2): 4 k = N - i - 1 5 A[i], A[k] = A[k], A[i] 6 return A Python is a very rich language and provides many built-in functions and methods. It turns out, that there is already a built-in method reverse, that solves this exercise. Using such a method, array A can be reversed simply by: 1 A.reverse