New to coding, What Am I missing? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
working on JS, What am I missing? Thank you
Modify the function below to greet only those with an even number of letters in their name
function helloYou(name)
numbers.filter (n => n % 2 =i= 1);{
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)

To access the number of letters in the string, you can use the attribute .length.
Then to check if this number is even a modulus 2 should return 0, that's what we need to check. This condition goes in an if statement.
Finally, if this condition is met, return Hello concatenated with name.
Otherwise nothing is returned, so it's undefined (there is no need to explicitly write return undefined).
function helloYou(name) {
if (name.length % 2 === 0) {
return "Hello, " + name;
}
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)

The variable numbers is actually undefined in this case.
Also, filter is not useful in this situation. Filter is mainly used to get the elements that match a condition from an array.
Your should use an if statement to check for even length. Better yet, you can use the ternary operator. Here is an example:
function helloYou(name) {
return name.length % 2 === 0 ? 'Hello, ' + name : undefined;
}
console.log(helloYou('Bob'));
console.log(helloYou('Anna'));

Related

How does the following code work step by step? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I came across this piece of code which checks if number of occurrences of an element in an array is greater than it is specified, and if yes it will remove the number:
function deleteNth(arr,x) {
var cache = {};
return arr.filter(function(n) {
cache[n] = (cache[n]||0) + 1;
return cache[n] <= x;
});
}
But I didn't understand the code from here: arr.filter(function(n){cache[n] = (cache[n]||0) + 1;return cache[n] <= x;});
Can anyone please explain in simple words what happens here and how does cache[n] part work.
Why is cache[n] incremented?
Thanks!
The arr.filter() begins by iterating over each item in the array and this case each item is represented by 'n'.
This item is then added to the empty object where 'n' is the key and the value is then incremented by one for each new item added to the object.
The return statement uses the cache to do a check of what 'n' values are less than or equal to x. If it returns false they are not added into the new array that is created. So if 'x' is 3 it will remove everything after the first three items from the array.
EDIT
Another way of writing the function which might make it more clear could be
function deleteNth(arr,x) {
return arr.filter((item, index) => {
if (index <= x) {
return item;
}
});
}

I was taking a challenge [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was taking a challenge and one of the questions seems like I got the right answer but it wouldn't pass. Need help understanding why it didn't.
Challenge: Add a method to the Person's prototype called "shoutName" that returns the person's name in all uppercase letters.
function Person(name) {
this.name = name;
this.shoutName = function() {
name.toUpperCase();
return '"' + name.toUpperCase()+'"'
}
}
/* Do not modify the code below this line */
const john = new Person('John');
console.log(john.shoutName(), '<-- should be "JOHN" ');
The question said to add a function to the constructor's prototype.
You didn't do that. You modified the constructor to dynamically add the function to the instance as the instance was created.
Person.prototype.shoutName = function () {
return this.name.toUpperCase();
}
Your function also wrapped the resulting value in quotes, which the question didn't ask you to do.
From your tiny picture, I noticed that your code was:
return '"' + name.toUpperCase() + '"';
Not sure why you added the quotes, just return this.name.toUpperCase(); and it should work fine. You should be referencing this object's property, rather than the input value of just name.
Also, having name.toUpperCase(); on a line by itself does nothing. Unnecessary calculations since that function returns a value that you're not assigning.

Javascript indexOf method [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I recently came across this little problem to solve on a website, counting the number of vowels in a string. I succeeded using a switch statement, but when I looked at the authors solution they had used the following function.
function vowel_count(str1) {
var vowel_list = 'aeiouAEIOU';
var vcount = 0;
for (var x = 0; x < str1.length ; x++) {
if (vowel_list.indexOf(str1[x]) !== -1) {
vcount += 1;
}
}
return vcount;
}
alert(vowel_count("The quick brown fox"));
Can anyone please explain what exactly is happening in the if statement,
I can see the index of whatever string that is passed to the function is being used but why would the statement == or !== -1. I'm a bit confused as to how the function is checking the string.
Thanks in advance.
The indexOf() function returns the index of an element we're looking for in a given array. However, if the element is nowhere to be found in the array, indexOf() returns -1 instead.
So in your case:
if (vowel_list.indexOf(str1[x]) !== -1) {
means something like "if the current letter can be found in my list of vowels".
Does that make sense ?
The indexOf method returns the position of the first occurrence of a specified value in a string. It returns -1 if the value to search for never occurs.
JavaScript, like most programming languages, starts counting from 0.
"foo".indexOf('f'); is 0.
The function returns -1 if the value isn't found.

javascript - check if string is in a array without case sensitive [duplicate]

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.
JS:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (input == array)
// I will do something.
} else {
// Something too
}
}
I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.
Sorry if this question is asked before. I searched for it but didn't know what keywords to use.
EDIT 1:
I am changing my question a little bit:
I want to check what is in my original question but with some other features.
I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.
Use indexOf:
if (array.indexOf(input) > -1)
It will be -1 if the element is not contained within the array.
This code should work:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) >= 0)
// I will do something.
} else {
// Something too
}
}
The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.
I think what you are looking for is
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) !== -1 )
// I will do something.
} else {
// Something too
}
}

Regex in JavaScript for first SUM/MIN/MAX/AVG found in string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What I'm trying to do is the following:
SUM(Sales) + SUM(Revenue)
or MIN(Sales) + SUM(Revenue).
What I want is the first calculation. So for 1), the result "SUM" will be given, and for 2) the result "MIN" will be given.
I've tried this for if statements but it's either impossible, or incredibly difficult to do that way. Could anyone guide me on potentially a RegEx way of doing this?
What I tried in if statements:
function hasFormula(formulaToLower) {
// formulaToLower could equal "SUM(Sales) + SUM(Revenue)" etc
// could also equal "SUM(Sales) + MIN(Revenue)" - this will return MIN, but it return SUM.
if (formulaToLower.indexOf('sum') !== -1) {
return "SUM";
}
if (formulaToLower.indexOf('min') !== -1) {
return "MIN";
}
}
Obviously though, this will bring out MIN first, even if it's found second, and so on...
You can use a regexp that allows all the combinations you want. The matches will be returned in the correct order if you use the global modifier, or only the first one will be returned if you do not:
var matcher = /SUM|MIN|MAX|AVG/;
var str1 = 'SUM(Sales) + SUM(Revenue)';
var str2 = 'MIN(Sales) + SUM(Revenue)';
console.log(str1.match(matcher)[0]) // SUM
console.log(str2.match(matcher)[0]) // MIN
*The [0] part takes the first element in the array of results returned by match.

Categories

Resources