Home > Web Front-end > JS Tutorial > body text

JavaScript Fun Question: Find the Murderer

黄舟
Release: 2017-02-15 14:30:38
Original
1234 people have browsed it

A key witness was murdered. Before he died, he tried to name the murderer, but his voice was weak and intermittent, making it difficult to distinguish.

We obtained his recording, represented by a string. This string starts with a letter, and each "~" symbol represents an uncertain letter.

The length of the string does not mean the length of the name, because he may die before saying the murderer's full name.

The police provided a list of suspects and asked you to help find the murderer based on the recording.

The result should be a string. If not found, return an empty string. If multiple are found, please return these and separate them with commas.


	
	var suspects=['Bernard Deltheil','Peter Gone','Peter Reeves','Roland Scorsini']
	
	sc('b~~~~~~~~~~~~~~l',suspects); //'Bernard Deltheil'
	sc('p~t~r',suspects);			 //'Peter Gone,Peter Reeves'
	sc('a~b~c~~d',suspects);         //''
Copy after login


Note:

1. Match each suspect’s name, ignoring case.

2. The code should be as short as possible

For the following implementation, I used regular expressions to first replace the "~" symbol within the string with ".", and then use it as Part of the pattern, matched with the name.


function sc(s,a){
    var r = [];
    a.forEach((e)=>{if(new RegExp("^" + s.replace(/~/g,"."),"i").test(e))r.push(e);});
    return r.join(",");
}
Copy after login


The above are fun JavaScript questions to find out the murderer. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template