Created
September 13, 2017 00:33
-
-
Save jochri3/8355898bfc03f5d636c6a8581ef71686 to your computer and use it in GitHub Desktop.
null created by ChrisLis - https://repl.it/HaLe/31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write a function that takes a string in and returns true if the letter | |
// "z" appears within three letters **after** an "a". You may assume | |
// that the string contains only lowercase letters. | |
// | |
// Difficulty: medium. | |
function nearby_az(string) { | |
var str=string | |
var tb=[] | |
for(var i in str) | |
{ | |
tb.push(str[i]) | |
} | |
for(var i in tb) | |
{ | |
if(tb[i]=='z') | |
{ | |
var tb_temp=tb.slice(0,i); | |
if(tb_temp.length<=3 && (tb_temp.includes('a'))) | |
{ | |
return true | |
break; | |
} | |
else | |
return false | |
break; | |
} | |
} | |
if(!(tb.includes('z'))) | |
{ | |
return false | |
} | |
} | |
// These are tests to check that your code is working. After writing | |
// your solution, they should all print true. | |
console.log("\nTests for //nearby_az") | |
console.log("===============================================") | |
console.log('nearby_az("baz") === true: ' + (nearby_az('baz') === true)) | |
console.log('nearby_az("abz") === true: ' + (nearby_az('abz') === true)) | |
console.log('nearby_az("abcz") === true: ' + (nearby_az('abcz') === true)) | |
console.log('nearby_az("a") === false: ' + (nearby_az('a') === false)) | |
console.log('nearby_az("z") === false: ' + (nearby_az('z') === false)) | |
console.log('nearby_az("za") === false: ' + (nearby_az('za') === false)) | |
console.log("===============================================") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment