prompt a string. identify it in an array, and if so, give back the info written as a fact of that string

write statements to declare the array and assign the values to the corresponding positions.
java programming exercises with solutions pdf
how to call a method with parameters in java
write a program that asks the user to type 10 integers of an array
method headers java
java loop exercises with solutions pdf
what's the telltale sign the code is a method?
string manipulation in c++ program

I have already defined an array. What I want to do is identify if the word given by the user is in my array and, if so, give the user back the info that the array has with regard that word (string)

var countryNameArray = [{
  name: 'France',
  fact: 'they speak french'
}, {
  name: 'Belgium',
  fact: 'they speak french or dutch'
},];

if (countryNameArray.indexOf(WordGiven[countryNameArray])) {
  console.info(countryNameArray.includes(WordGiven));
  alert(countryNameArray.name, + + , countryNameArray.fact)
} else {
  alert ('I did not understand. Please give me a Country Name')
};

To be clear, what you're asking is that given a string, you would like to get the object inside countryNameArray whose name matches the given string.

One way to achieve that is using filter.

var countryNameArray = [
    {
        name: 'France',
        fact: 'they speak french'
    },
    {
        name: 'Belgium',
        fact: 'they speak french or dutch'
    },
];

var matchingCountries = countryNameArray.filter(c => c.name === WordGiven);

From filter, you will have an array where name matches WordGiven, from which you can handle in any way you'd like, for example

if (matchingCountries.length === 0) {
    console.log('No matching country found');
} else {
    var firstMatch = matchingCountries.first();
    console.log(firstMatch.name, + + , firstMatch.fact);
}

EDIT: After seeing the other answer using find, that is a much better fit for what you're trying to achieve. Substitute filter for find and you wouldn't need to do the whole .first() stuff.

Java Basics Exercises - Java Programming Tutorial, Write a program called CheckPassFail which prints " PASS " if the int variable " mark Learning programming is like learning cycling, swimming or any other sports. Take note that computer programs may not produce the correct result even though coding pattern which involves a boolean flag to check the input string.

You can use .find() to return the object with name matching your user input. Then you can use destructing assignment to get the properties (name and fact from your javascript object) from your found object.

See working example below:

const countryNameArray = [{
    name: 'France',
    fact: 'they speak french'
  },
  {
    name: 'Belgium',
    fact: 'they speak french or dutch'
  },
],
wordGiven = prompt("Enter a country name");

if(countryObj = countryNameArray.find(({name}) => name === wordGiven)) {
  const {name, fact} = countryObj; // get the name and fact properties from countryObj
  alert(name + ': ' + fact);
} else {
  alert('I did not understand. Please give me a Country Name');
}

Writing Methods, Its statements use the parameter names just like variable names; in fact, we often call Parameter variables are used to convey special information to the method; Prompt; public class Application { public static void main(String[] args) { int process, Java eventually gets back to the special main method, and if there is no​ 

By creating a function and passing the country name as a parameter we can achieve this using the find() method.

const countryNameArray = [{
    name: 'France',
    fact: 'they speak french'
},
{
    name: 'Belgium',
    fact: 'they speak french or dutch'
    },
];

const result = (name) => {
    let country = countryNameArray.find(obj => obj.name === name);
    if (country) {
        alert(`${country.name} ${country.fact}`); // 'France they speak french'
    } else {
        alert ('I did not understand. Please give me a Country Name')
    }
}

result('France');

Chapter 7: Arrays, Like Strings, arrays use zero-based indexing, that is, array indexes start with 0. Write a program that inputs the length of a int array from the user and assigns 1 at index 0, One loop should assign values to each element of the array. nextInt( ); // Declare and create an array, maybe should check if days is positive int[ ] 

Strings and string manipulation in C++, We now use string in a similar way as built-in data types, as shown in the example C++ strings also provide many string manipulation facilities. To do that, we use the square brackets, as if we were dealing with an array of characters, or a If the string does not contain any space characters, the result of the find method 

Program to remove vowels from a String, Given a string, remove the vowels from the string and print the string without vowels. Please write comments if you find anything incorrect, or you want to share more information about Don't stop now and take your learning to the next level. first_page Check if LCM of array elements is divisible by a prime number or not.

Recursive Programs to find Minimum and Maximum elements of array, It contains well written, well thought and well explained computer science and Given an array of integers arr, the task is to find the minimum and maximum element of that array using recursion. Recursive call: If the base case is not met​, then call the function by passing the array of If there is single element, return it​.

Comments
  • actually, your solution with .filter() isn't bad as the countryNameArray might have many objects which have the same name. However, if you do use this, it would be better to looper through the matchingCountries array to output all matches, rather than just the first one ;)
  • @NickParsons Agreed, however I was trying to emphasise more on the .filter() part and sort of say after that "you can do whatever you like, for example...". Maybe I should change the example then to highlight what you've said.