Interview Question #1:
Write a function that counts all vowels in a sentenceβπ€
If you need practice, try to solve this on your own. I ...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Another option - Using array reduce method
function getVowelsCount(str) {
const vowels = ["a", "e", "i", "o", "u"];
return str.split('').reduce((prev, ch, idx) => (vowels.includes(ch.toLowerCase())) ? prev + 1 : prev, 0);
}
thanks for the snippet. I tend to stay away from
~though as it is not commonly used and is not readable.However, we can prolly ask if interviewer is interested on some fancy code and may receive a bonus point for it.
As for staying away from language features because they are 'not commonly used' - I personally think that is a terrible idea. We end up writing code for the lowest common denominator of understanding - something that can only result in code becoming unnecessarily verbose, devoid of elegance, and possibly efficiency (see your example above where you call
matchtwice). It's the equivalent of writing versions of adult literature for slow readers. Dumbing down has its place, but should only be done where appropriate.Way too much emphasis is put on making things 'easy' for the developer.
i see valid points here.
Γ believe that it should be easy for developer as they will be iterating on codebase over and over which will always get my vote. let build tool over optimize as it is one of their main job.
i would definitely improve the match example. didnt make it the most optimized version here however i mentioned it on the youtube video though. ππ i would continue to post readable bits like this though to capture ALL level of skill as why I exist here, to be able to share to all level of experience
This kind of thinking is also part of the problem. If everyone has this attitude, how will anything get any better? Who builds the build tools? Who will maintain them once the creators have passed on?
An over reliance on perceived simplicity is very dangerous
Sorry to vent all this on you, I know you're just trying to make tutorials for beginners :P
Really finding these tutorials helpful. On the second solution, there is an array of vowels thats not being used. Also, the char.toLowercase() does not seem to be needed because there is already the i in the regex.
thank you! tutorials are meant to see some patterns that you can use for interviews. Arrays of vowels are used to compare it with the sentence. we loop over the sentence one by one which is assigned to variable char AND we need to transform it as lowercase as sentence may come up as
HeLLo WORLD. we know thatoandOis not equal. hopefully this makes sense. let me know if it didn'tnice and sweet! thanks for the snippet.
Consider some "y". Thanks for the 10 react refresher samples. Been away a couple months and your tests really helped wake me up. Very much appreciated!!!