1

I'm trying to complete an assignment using every, I tried everything and after and after 1 hour I decided to ask your help.

This function should return true if all the words in the array start with a case-insensitive but always return false, no matter what.

Thank you

var allStartingWithA = function(words){
  return words.length !== 0? words.every((word) => {word[0] === /"a"/i }) : true;
};

allStartingWithA(["abbb","aee", "aee", "avvv"]);
Marco
  • 65
  • 1
  • 8
  • `word[0]` is a `string`, but you are comparing it to a `RegExp`, which always result `false`. – tsh Dec 05 '17 at 05:17

4 Answers4

1

Issue is you are not returning anything from callback.

Anonymous functions using arrow function can be written as

() = > expression

or

() => { function body; }

Approach 1 by defaults return the value of expression and approach 2 by defaults return undefined.

Since you are using second approach, you will either have to manually return value or switch to first approach.


Another problem in your code is expression. When you have to test using a regex, you should use method test.

So your expression should be /^a/i.test(word).

If you want to compare first character only, you can so word[0] === "a" but this will be case sensitive.


Another optimization can be, since you want to return true if array is empty, you can return an expression that evaluates as:

  • Either array should be blank or all words should start with some characters.

So your final code would be:

var allStartingWithA = function(words) {
  return words.length === 0 || words.every((word) => /a/i.test(word));
};

console.log(allStartingWithA([]));
console.log(allStartingWithA(["abbb", "aee", "aee", "avvv"]));
console.log(allStartingWithA(["bbb", "aee", "aee", "avvv"]));

References:

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
1

Someone before posted this and then delete it, I like this solution and thank you whoever you are.

var allStartingWithA = function(words){
  return words.length !== 0? words.every(word => word[0].toLowerCase()=='a') : true;
};
console.log(allStartingWithA(["abbb","aee", "aee", "avvv"]));
Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31
Marco
  • 65
  • 1
  • 8
  • This is the profile link of person you wish to thank: https://stackoverflow.com/users/4964923/jaydip-jadhav – Rajesh Dec 05 '17 at 05:22
0
  1. Why are you returning true if length is 0 ? You should return false instead.
  2. If you're going to use regex, it's better to use match() or test(). Or you can just use .toLowerCase().
  3. When testing your code, use different scenarios to make sure your code works correctly. ["abbb","aee", "aee", "avvv"] always gonna return true. Try adding strings starting with A or starting with other letters to test that your function will return false as well.

var allStartingWithA = function(words){
  return words.length !== 0? words.every((word) => word[0].toLowerCase() == 'a' ) : false;
};

allStartingWithA(["abbb","aee", "aee", "avvv"]);
allStartingWithA(["Abbb","aee", "aee", "avvv"]);
allStartingWithA(["notStartingWithA","aee", "aee", "avvv"]);
IamMowgoud
  • 308
  • 4
  • 13
0

Your approach is correct for the problem. Whereas you did small mistakes with your arrow function usage and use of regex

Firstly (word) => {word[0] === /"a"/i } function doesn't return anything

You can use

(word) => {return word[0] === /"a"/i } or (word) => word[0] === /"a"/i

And secondly you are doing just comparison. Then if you add //i it will become regex then you need to use test method.

So use return word[0] === "a" simply for comparison

var allStartingWithA = function(words){
  return words.length !== 0? words.every((word) => word[0] === "a") : true;
};

console.log(allStartingWithA(["abbb","aee", "aee", "avvv"]));
Shalitha Suranga
  • 1,138
  • 8
  • 24