Complete the function given below that takes any string as input and removes all the vowels
and spaces from it and returns the remaining string.
Sample input - removeVowelsAndSpaces(‘arijit kumar’)
output - 'rjtkmr
gives an error dont know why but the ide uses node.js for processing using module.export
What code have you written and what error/s are you getting? And with your current questions, we don’t have enough context to know what you already know or don’t know , so it is impossible to guide you.
let removeVowelsAndSpaces = function(givenString){
// start your code here.
let vowels = {
'a': true,
'e': true,
'i': true,
'o': true,
'u': true
};
let result = "";
for (let i = 0; i < string.length; i++) {
let letter = string[i].toLowerCase();
if (!vowels[letter]) {
result += string[i];
}
}
return result;
};
module.exports = { removeVowelsAndSpaces: removeVowelsAndSpaces };
**
**dont know what is the error always showing an error message ;like something is still missing?
Where exactly does string come from? Isn’t it supposed to be givenString?
its an online test actually its just a function and their IDE has set of rules which i have to follow but the code is not runnning
please help me out here
let vowels = { ‘a’: true, ‘e’: true, ‘i’: true, ‘o’: true, ‘u’: true };
let stringArray =givenString.split("");
let result="";
for (let i = 0; i < stringArray.length; i )
{
let letter = stringArray[i].toLowerCase();
if (!vowels[letter])
{
res = stringArray[i];
}
else
{
result = givenString.replace(/[aeiou]/gi, ‘’);
}
}
};
There’s no need to split since JS indexes characters the same way arrays do.
let string = 'string';
console.log(string[0]);
// outputs letter 's'
Problem with the first code sample is that variable string wasn’t assigned. Either set string=givenString or pass givenString directly to for loop.
let removeVowelsAndSpaces = function(givenString)
{
// start your code here.
let vowels = { ‘a’: true, ‘e’: true, ‘i’: true, ‘o’: true, ‘u’: true };
let t = givenString.replace(’ ', ‘’);
let result = “”;
for (let i = 0; i < t.length; i++) {
let letter = t[i].toLowerCase();
if (!vowels[letter]) {
result = result + t[i];
}
}
return result;
};
It works, but be more careful with quotation marks and apostrophes since characters you’ve typed aren’t considered those.
let removeVowelsAndSpaces = function(givenString){
// start your code here.
let vowels = { 'a': true, 'e': true, 'i': true, 'o': true, 'u': true };
let t = givenString.replace(' ', '');
let result = '';
for (let i = 0; i < t.length; i++) {
let letter = t[i].toLowerCase();
if (!vowels[letter]) {
result = result + t[i];
}
}
return result;
};
Also I think that your first attempt was a bit better solution, with slight modifications of course.
let removeVowelsAndSpaces = function(givenString){
// start your code here.
let vowels = {
'a': true,
'e': true,
'i': true,
'o': true,
'u': true,
' ': true,
};
let result = "";
for (let i = 0; i < givenString.length; i++) {
let letter = givenString[i].toLowerCase();
if (!vowels[letter]) {
result += givenString[i];
}
}
return result;
};
BUT COMPILER SHOWS AN ERROR MESSAGE WITH REGARDS TO REMOVAL OF WHITE SPACESstrong text
What error exactly, could you take a screenshot of the error message?
I remember having issues with similar errors previously, not on your course though since I don’t think that I’ve taken it. It’s possible that your program is working but that your solution isn’t their solution and even things like spaces and indentation can be reported as errors. I’d try to look up their solution and see if you made some mistakes which doesn’t seem likely or simply move on.
but i made a mistake i paid them for the course and have to clear all the assignment to land a job
Another possibility is that module thing which I assume comes from some framework like React which I have no experience with. Your code works.