Iteration through string and remove all instances of - javascript

My instructions were to iterate through a string and remove all instances of the letter "a". I thought that it would be easy to find examples, but I was unable to do so. Some would remove the letter without the iteration, but that is not what the instructions asked. If someone could please look at my code and assist me in my task I would greatly appreciate it! The "removeA" function will iterate through the string now, and only console logs the !== "a", but for the life of me I can't figure out how to save it to a new string. Thanks in advance.
removeA = function(stringWithA) {
if (stringWithA === null || typeof (stringWithA) !== "string" || stringWithA === "") { //Checking for is null AND is not array
return 'Please enter a valid string';
} else {
lowerWithA = stringWithA.toLowerCase();
for (var i = 0; i < lowerWithA.length; i++) {
if (lowerWithA.charAt(i) !== "a") {
console.log(lowerWithA.charAt(i));
}
}
}
}

You can store the letters into an Array.
var removeA = function(stringWithA) {
if (stringWithA === null || typeof(stringWithA) !== "string" || stringWithA === "") { //Checking for is null AND is not array
return 'Please enter a valid string';
} else {
var newString = [];
lowerWithA = stringWithA.toLowerCase();
for (var i = 0; i < lowerWithA.length; i++) {
if (lowerWithA.charAt(i) !== "a") {
newString.push(lowerWithA.charAt(i))
}
}
return newString.join('');
}
}
console.log(removeA("Eleazar"))
Or, just use a regex:
var removeA = function(stringWithA) {
if (stringWithA === null || typeof(stringWithA) !== "string" || stringWithA === "") { //Checking for is null AND is not array
return 'Please enter a valid string';
} else {
return stringWithA.replace(/a/gi, '')
}
}
console.log(removeA("EleaaaaazAreeeeaaaElAAAAAeaaaEleEvene"))

Why not build a new string with all the characters that are not a?
var newString = "";
for (var i = 0; i < lowerWithA.length; i++) {
var letter = lowerWithA.charAt(i);
if (letter !== "a") {
newString += letter;
}
}
console.log(newString);
If you wanted to expand this to be case-insensitive:
...
if (letter !== 'a' || letter !== 'A') { ... }
And simply don't call String.toLowerCase() on the original string.

I suppose there's already a function that you need, replace:
var stringWithA = 'A aaaa bbbcc!';
alert(stringWithA.replace(/[Aa]/g, ''));

Related

Return "True" if all the characters in a string are "x" or "X" else return false

I am looking at this code challenge:
Complete the function isAllX to determine if the entire string is made of lower-case x or upper-case X. Return true if they are, false if not.
Examples:
isAllX("Xx"); // true
isAllX("xAbX"); // false
Below is my answer, but it is wrong. I want "false" for the complete string if any of the character is not "x" or "X":
function isAllX(string) {
for (let i = 0; i < string.length; i++) {
if (string[i] === "x" || string[i] === "X") {
console.log(true);
} else if (string[i] !== "x" || string[i] !== "X") {
console.log(false);
}
}
}
isAllX("xAbX");
Your loop is outputting a result in every iteration. There are two issues with that:
You should only give one result for an input, so not in every iteration; currently you are reporting on every single character in the input string.
You are asked to return a boolean result (false/true), not to have the function print something. That should be left to the caller
You could take a simpler approach though, and first turn the input string to all lower case. Now you only have to look for "x". Then take out all "x" and see if something is left over. You can check the length property of the resulting string to decide whether the return value should be false or true:
function isAllX(string) {
return string.toLowerCase().replaceAll("x", "").length == 0;
}
console.log(isAllX("xxXXxxAxx")); // false
console.log(isAllX("xxXXxxXxx")); // true
If you are confortable with regular expressions, you could also use the test method:
function isAllX(string) {
return /^x*$/i.test(string);
}
console.log(isAllX("xxXXxxAxx")); // false
console.log(isAllX("xxXXxxXxx")); // true
You can try this way.
function isAllX(str) {
let isX = true;
let newString = str.toLowerCase();
for (let i = 0; i < newString.length; i++) {
if (newString[i] !== "x") {
isX = false;
}
}
return isX;
}
console.log(isAllX("xAbX"));
console.log(isAllX("XXXxxxXXXxxx"));
You can use regex to find the same.
function allX(testString) {
return /^x+$/i.test(testString);
}
console.log(allX("xxXX"));
console.log(allX("xxAAAXX"));
Without any method if you want
function isAllX(str) {
let flag = true;
for (let i = 0; i < str.length; i++) {
if (str[i] !== "x" && str[i] !== "X") {
flag = false;
// break;
}
}
return flag;
}
console.log(isAllX("xAbX"));
console.log(isAllX("XXXxxxXXXxxx"));
console.log(isAllX("xx"));
You can try converting the string to a single case, then looping over it while checking for the condition as below
function isAllX(string) {
const newString = string.toUpperCase();
for (let i = 0; i < newString.length; i++) {
if (newString[i] !== "X") {
return false
}
}return true
}

Valid Parentheses. Gives a wrong boolean (JS problems on LeetCode)

Trying to solve a problem at LeetCode called "Valid Parentheses".
Conditions are:
Given a string s containing just the characters '(', ')', '{', '}',
'[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets. Open
brackets must be closed in the correct order.
I wrote a function and it's working in this snippet:
let arr = ['()']
var isValid = function (data) {
let stack = [];
const bracketsArray = {
'{':'}',
'[':']',
'(':')'
}
for (i=0; i < data[0].length; i++) {
if (data[0][i] == '{' || data[0][i] == '(' || data[0][i] == '[') {
stack.push(data[0][i]);
} else if (data[0][i] == '}' || data[0][i] == ']' || data[0][i] == ')') {
if (bracketsArray[stack[stack.length-1]] == data[0][i]) {
stack.pop()
}
}
}
if (stack.length == 0) {
return true
} else {
return false
}
}
console.log(isValid(arr))
Function gives a correct output (boolean)
But when I run this code at LeetCode for some reason the same code gives me a wrong boolean.
Don't understand what is wrong.
/**
* #param {string} s
* #return {boolean}
*/
var isValid = function(s) {
let stack = [];
const bracketsArray = {
'{':'}',
'[':']',
'(':')'
}
for (i=0; i < s[0].length; i++) {
if (s[0][i] == '{' || s[0][i] == '(' || s[0][i] == '[') {
stack.push(s[0][i]);
} else if (s[0][i] == '}' || s[0][i] == ']' || s[0][i] == ')') {
if (bracketsArray[stack[stack.length-1]] == s[0][i]) {
stack.pop()
}
}
}
if (stack.length == 0) {
return true;
} else {
return false;
}
};
Any advice?
On LeetCode the function parameter is a string, but in your tests you are passing an array, and also your function code expects an array, since it accesses the string with s[0], instead of s.
Unrelated, but:
Your code fails when the input is just a closing bracket. This is because your loop doesn't break with return false when a non-matching closing bracket is encountered. This should happen in the else part of if (bracketsArray[stack[stack.length-1]] == s[i]) {
Don't use an undeclared variable i, which will then implicitly become a global variable (if running in non-strict mode).
Don't name your variable bracketsArray since it is not an array.
Make more use of that object, instead of making three comparisons with opening brackets.
The final if...then is overkill for just returning the value of a boolean expression.
So:
var isValid = function(s) {
const stack = [];
const brackets = {
'{':'}',
'[':']',
'(':')'
}
const closing = Object.values(brackets);
for (let ch of s) {
if (brackets[ch]) {
stack.push(brackets[ch]);
} else if (ch == stack.at(-1)) {
stack.pop()
} else if (closing.includes(ch)) {
return false;
}
}
return !stack.length;
};
console.log(isValid("{([])}")); // true
console.log(isValid("]")); // false
The way you are accessing the current character of the iteration is wrong. Instead of doing s[0][i], simply do s[i].
s[0][i] accesses the first character, and then attempts to get the i-th entry from it (which is undefined).
var isValid = function(s) {
let stack = [];
const bracketsArray = {
'{':'}',
'[':']',
'(':')'
}
for (i=0; i < s.length; i++) {
if (s[i] == '{' || s[i] == '(' || s[i] == '[') {
stack.push(s[i]);
} else if (s[i] == '}' || s[i] == ']' || s[i] == ')') {
if (bracketsArray[stack[stack.length-1]] == s[i]) {
stack.pop()
}
}
}
if (stack.length == 0) {
return true;
} else {
return false;
}
};
isValid("{([])}") // true
isValid("{([)}") // false

Valid Parenthesis Troubleshooting

I'm working on a coding challenge that involves valid parenthesis and I'm trying to troubleshoot why it's not giving me the intended response.
function isValid(str) {
if (str.length === 0)
return true
let matchingOpeningBracket, char
let stack = []
let openingBrackets = ['[', '<', '(']
let closingBrackets = [']', '>', ')']
for (let i = 0; i < str.length; i++) {
char = str[i]
if (closingBrackets.indexOf(char) > -1) {
matchingOpeningBracket = openingBrackets[closingBrackets.indexOf(char)]
if (stack.length == 0 || (stack.pop() != matchingOpeningBracket)) {
return false
}
} else {
stack.push(char)
}
}
return (stack.length == 0)
};
When I try to check console.log(isValid('---(++++)---')) and console.log(isValid('before(middle[])after')) it gives me a false value but it should be passing as true according to the challenge key. I'm not sure what step I'm missing. The other tests examples are logging as intended as seen here except for the first 2.
//true // returns false
// let example1 = ("before(middle[])after")
//true //returns false
// let example1 = ("---(++++)---")
//true
// let example1 = ("")
//false
// let example1 = (")(")
//false
// let example1 = ("<(>)")
//true
// let example1 = ("([<>()])")
//false
// let example1 = ("([)")
You are pushing everything that is not a closing bracket onto the stack when you should ignore chars that are neither opening or closing brackets. Change your else condition to this:
else if (openingBrackets.indexOf(char) > -1) {
I definitely would not want to complicate things so I am giving a simple solution, so if you see it fit you can use it :)
function isValid(str) {
if (str.length === 0)
return true
let char;
let stack = [];
let openingBrackets = ['[', '<', '(']
let closingBrackets = [']', '>', ')']
matchesOpen = (val) => (val === '[' || val === '<' || val === '(')
matchesclose = (val) => (val === ']' || val === '>' || val === ')')
getOpeningBracket = (val) => {
switch (val) {
case ']':
return '[';
case ')':
return '(';
case '>':
return '<';
default:
return '';
}
}
for (let i = 0; i < str.length; i++) {
char = str[i]
if (matchesOpen(char)) {
stack.push(char);
} else if (matchesclose(char)) {
if (stack.pop() !== getOpeningBracket(char)) return false;
}
}
return (stack.length == 0)
};
console.log(isValid(('---(++++)---')))
console.log(isValid('before(middle[])after'))
console.log(isValid("---(++++)---"))
You’re pushing onto the stack every char that is not an opening parenthesis. You only want to push opening parentheses onto the stack.
Notice that you get a false result whenever there are non-parenthesis characters in the string.

How to replace undefined value with a string

I tried to use this code:
if((data.memberData.workStreak || 0) >= 5){
won+=400;
embed.addField(message.language.get("WORK_CLAIMED_HEADINGS")[0], message.language.get("WORK_CLAIMED_SALARY", won))
.addField(message.language.get("WORK_CLAIMED_HEADINGS")[1], message.language.get("WORK_AWARD"));
data.memberData.workStreak = 0;
} else {
for(let i = 0; i < award.length; i++){
if (typeof i === 'undefined') {
return '<:b_:682865394637078531>';
}
if(data.memberData.workStreak > i){
let letter = Discord.Util.parseEmoji(award[i]).name.split("_")[1];
award[i] = ":regional_indicator_"+letter+":";
}
}
But it did absolutely nothing.
Any help?
You can simply use the OR operator and print string instead of undefined. In below statement if a is undefined then it will store empty string in a instead of undefined.
a = a || ''
Replace your line with below line:
let letter =Discord.Util.parseEmoji(award[i]).name;
OR
let letter =Discord.Util.parseEmoji(award[i]);
Please find working snippit below:
const a = undefined;
console.log(a);
console.log(a || '');
I don't know exactly, what this little code snippet is aimed to do.. But may this could help you?
let letter = Discord.Util.parseEmoji(award[i]).name.split("_")[1];
if (letter == undefined) {
return '<:b_:682865394637078531>';
}
if(data.memberData.workStreak > i){
award[i] = ":regional_indicator_"+letter+":";
}

Cannot read property length null error when used with regular expressions

I'm a javascript beginner doing some CodeWars.com questions. I came across this question and I'm stuck due to a "cannot read property length null" error. I've tried to look up that error and can't find what the problem is in my program.
The assignment is:
"Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contains any char."
And this is what I've written so far:
function XO(str) {
var x = "x";
var o = "o";
var numX = str.match(/x/gi).length;
var numO = str.match(/o/gi).length;
while(str.indexOf(x) > -1 || str.indexOf(o) > -1) {
if(numX == numO){
return true;
}
}
if (numX === -1 && numO === -1){
return true;
}
}
XO("xoxo");
The assignment also says that if there is neither an X or an O then the program should return true.
This will not give you that error. When there are no matches, the match function returns null and you cannot get the length of null. A few extra lines solves this issue.
function XO(str) {
var x = "x";
var o = "o";
var numX = 0;
var numO = 0;
var xMatch = str.match(/x/gi);
var oMatch = str.match(/o/gi);
if (xMatch) {
numX = xMatch.length;
}
if (oMatch) {
numO = oMatch.length;
}
while(str.indexOf(x) > -1 || str.indexOf(o) > -1) {
if(numX == numO){
return true;
} else {
return false;
}
}
if (numX === -1 && numO === -1){
return true;
} else {
return false;
}
}
console.log(XO("ddd"));
I think you are making this problem more complex than it has to be.
All you need to do is make the string lowercase(to account for case insensitive), traverse the string, and when it finds an x, add 1 to a counter, and when you find and o, decrease 1 from the counter.
If it ends at 0, you return true, else you return false. There's no need for regexes
function XO(str){
var count = 0;
str = str.toLowerCase();
for(var i = 0; i < str.length; i++){
if(str[i] === 'x') count++;
if(str[i] === 'o') count--;
}
return count === 0 ? true : false;
}
Yes you have to check the return value of match is not null before checking the length property. However
while(str.indexOf(x) > -1 || str.indexOf(o) > -1) {
if(numX == numO){
return true;
}
}
looks like an infinite loop if either string contains lower case 'x' or 'o' and there are a different number of each.
More simply:
function XO(str)
{ var matchX = str.match(/x/gi);
var matchY = str.match(/o/gi);
return (matchX && matchY) ? matchX.length == matchY.length : !matchX && !matchY;
}

Categories

Resources