Jump to content

The Name Game: Difference between revisions

JavaScript added
(JavaScript added)
Line 1,257:
Fee-fi-mo-mteve
Steve!</pre>
 
=={{header|JavaScript}}==
<lang javaScript>function singNameGame(name) {
 
// normalize name
name = name.toLowerCase();
name = name[0].toUpperCase() + name.slice(1);
 
// ... and sometimes y
// let's pray this works
let firstVowelPos = (function() {
let vowels =
'aeiouàáâãäåæèéêëìíîïòóôõöøùúûüāăąēĕėęěĩīĭįıijōŏőœũūŭůűų'
.split('');
function isVowel(char) {
return vowels.indexOf(char) >= 0;
}
if (isVowel(name[0].toLowerCase())) return 0;
if (name[0] == 'Y' && !isVowel(name[1])) return 0;
if (name[0] == 'Y' && isVowel(name[1])) return 1;
vowels = vowels.concat(vowels, 'yÿý'.split(''));
for (let i = 1; i < name.length; i++)
if (isVowel(name[i])) return i;
})();
 
let init = name[0].toLowerCase(),
trunk = name.slice(firstVowelPos).toLowerCase(),
b = trunk, f = trunk, m = trunk;
 
switch (init) {
case 'b': f = 'f' + trunk; m = 'm' + trunk; break;
case 'f': b = 'b' + trunk; m = 'm' + trunk; break;
case 'm': b = 'b' + trunk; f = 'f' + trunk; break;
}
 
return `
<p>${name}, ${name}, bo-${b}<br>
Banana-fana fo-${f}<br>
Fee-fi-fo-mo-${m}<br>
${name}!<br></p>
`
}
 
// testing
let names =
'Gary Earl Billy Felix Mary Christine Brian Yvonne Yannick'.split(' ');
for (let i = 0; i < names.length; i++)
document.write(singNameGame(names[i]));</lang>
{{out}}
Gary, Gary, bo-ary
Banana-fana fo-ary
Fee-fi-fo-mo-ary
Gary!
 
Earl, Earl, bo-earl
Banana-fana fo-earl
Fee-fi-fo-mo-earl
Earl!
 
Billy, Billy, bo-illy
Banana-fana fo-filly
Fee-fi-fo-mo-milly
Billy!
 
Felix, Felix, bo-belix
Banana-fana fo-elix
Fee-fi-fo-mo-melix
Felix!
 
Mary, Mary, bo-bary
Banana-fana fo-fary
Fee-fi-fo-mo-ary
Mary!
 
Christine, Christine, bo-istine
Banana-fana fo-istine
Fee-fi-fo-mo-istine
Christine!
 
Brian, Brian, bo-ian
Banana-fana fo-fian
Fee-fi-fo-mo-mian
Brian!
 
Yvonne, Yvonne, bo-yvonne
Banana-fana fo-yvonne
Fee-fi-fo-mo-yvonne
Yvonne!
 
Yannick, Yannick, bo-annick
Banana-fana fo-annick
Fee-fi-fo-mo-annick
Yannick!
 
 
=={{header|Julia}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.