0

Still new to javascript and having a lot of trouble with arrays within arrays. I am tasked to pull names and quotes out of two arrays. I am tasked with making a function that can have a quote inputed into it and spit out the response "John says: Blah blah blah" They're set up as such.

let names = [ John, James, Joe, Justin] 
let quotes = [ 
[ "Help me." 
 "Blah blah blah blah"
  "Blah blah blah" ] 
 [ "Blah blah blah" 
   "Quote here" ]] 

Now there are more names and quotes than that I just stopped for brevity's sake. The names and quotes both match up in the sense that the first name in the names array corresponds with the first array of quotes in the quotes array. Heres my attempt at writing code for it.

 function whoSaidThis (quote) {
 var index = quotes.indexOf(quote);
 return crewmembers.index 
 }

this comes back as undefined and I can't figure out why.

2
  • crewmembers isn't defined anywhere, hence undefined. Commented Oct 4, 2017 at 18:07
  • (perhaps crewmembers is supposed to be names?) Also, are you missing some ,'s in your quotes list ? Commented Oct 4, 2017 at 20:37

4 Answers 4

1

Why undefined is returned :

Your function returns undefined because you have nowhere initialized property index of object crewmembers, thus you are trying to access a property of crewmembers that doesn't exist thus undefined is returned.

It's Javascript way of telling you "Hey index property doesn't exist for crewmembers object"

Also,in regards to your attempt, you are trying to search for the qoute within the qoutes array, instead you should actually search for qoute inside each item of qoutes array.

and when you hit a match you return the index of that item array, this index is the index of that item array inside qoute array.

You could do something like this :

var names = ["John", "James", "Joe", "Justin"];
var quotes = [["Help me.", "Blah blah blah blah", "Blah blah blah"],["Blah blah blah", "Quote here"],["sample qoute"],["sample qoute again"]];

function whoSaidThis(qoute){
    //Iterate over qoutes array
    for(var i=0;i<quotes.length;i++) {
        //check if any of the items in qoutes array contain the qoute 
        if(quotes[i].indexOf(qoute) !== -1 ) {
            break;
        }
    }
    //returns undefined if qoute not foundin the qoutes array
    return i === quotes.length ? undefined : names[i] + " says : " + qoute;
};

console.log(whoSaidThis("sample qoute"));

Sign up to request clarification or add additional context in comments.

Comments

0

You could seach with Array#findIndex and Array#includes and take the index of the array for returning the corresponding name.

function whoSaidThis(quote) {
    var index = quotes.findIndex(q => q.includes(quote));
    return names[index];
}

let names = ["John", "James", "Joe", "Justin"],
    quotes = [
        ["Help me.", "Blah blah blah blah", "Blah blah blah"],
        ["Blah blah blah", "Quote here"]
    ];

console.log(whoSaidThis("Quote here"));

Comments

0

This code isn't actually solving your problem. But it might help.

Note that i`m using ES6 here. But you can easily transpile it to ES5

const personsQuotes = {
  'John': ['bla', 'Yeah', 'No'],
  'James': ['qew', 'ert', 'dasd'],
  'Joe': ['ok', 'ko', 'oko'],
  'Justin': ['ZZz', 'zzz', 'bla']
}

function whoSaidThis(phrase) {
  let ans = [];

  for(person in personsQuotes) {
    personsQuotes[person].forEach((quote) => {
      if (quote === phrase)
        ans.push(person);
    });
  }

  return ans.length ? ans : null;
}

console.log( whoSaidThis('kek') );
console.log( whoSaidThis('ok') );
console.log( whoSaidThis('bla') );

Comments

0

I think you are looking for is something like this. In your example multiple people say the same thing, but the output is a single name. You would just keep an array in whoSaid and return/print that instead and return ['nobody'] if the array is empty at the end of the for loop.

let names = [ 'John', 'James', 'Joe', 'Justin'];
let quotes = [ 
  ["Help me.",
  "Blah blah blah blah",
  "Blah blah blah"],
  [ "Blah blah blah", 
    "Quote here" ]];

function whoSaid(quote){
  for(var nameIndex = 0; nameIndex < quotes.length; nameIndex++){
    if(quotes[nameIndex].indexOf(quote) !== -1){
      return names[nameIndex];
    }
  }
  return 'nobody';
}

function printQuote(quote){ 
  console.log(whoSaid(quote), ' says: ', quote);
}

printQuote('Help me.' );
printQuote('Quote here' );
printQuote('Nobody said that');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.