Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions codewars/6kyu/what-s-a-name-in/koronya.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// [JS][6kyu] What's A Name In?
// what-s-a-name-in
// https://www.codewars.com/kata/59daf400beec9780a9000045/train/javascript

const findAllIndex = (str, char) => {
const result = []
for (let i = 0, strLen = str.length; i < strLen; i += 1 || 0) {
if (str[i].toLowerCase() === char.toLowerCase()) {
result.push(i)
}
}
return result
}

const nameInStr = (str, name) => {
console.log(`str: ${str}, name: ${name}`)
const arr = name.split('').map((char, index) => findAllIndex(str, char))
let prevMax = -1
return arr.every((item) => {
const next = item.find((num) => num > prevMax)
prevMax = next
return next !== undefined ? true : false
})
}

// nameInStr('Across the rivers', 'chris') === true
// nameInStr('Next to a lake', 'chris') === false
// nameInStr('Under a sea', 'chris') === false
// nameInStr('A crew that boards the ship', 'chris') === false
// nameInStr('A live son', 'Allison') === false
// nameInStr('thomas', 'Thomas') === true