Skip to content

Commit 134cbfa

Browse files
authored
Create Indexedcapitalization.js
1 parent aa3c859 commit 134cbfa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: Indexedcapitalization.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Given a string and an array of integers representing indices, capitalize all letters at the given indices.
2+
3+
// For example:
4+
5+
// capitalize("abcdef",[1,2,5]) = "aBCdeF"
6+
// capitalize("abcdef",[1,2,5,100]) = "aBCdeF". There is no index 100.
7+
// The input will be a lowercase string with no spaces and an array of digits.
8+
9+
function capitalize(s,arr){
10+
let arrStr = s.split('')
11+
for(let i = 0; i < arr.length; i++){
12+
//if the arr value is less than arr string length
13+
if(arr[i] < arrStr.length){
14+
//uppercase the value in the arrStr
15+
arrStr[arr[i]] = arrStr[arr[i]].toUpperCase()
16+
}
17+
}
18+
//join the arrStr
19+
return arrStr.join('')
20+
};
21+
22+
23+
function capitalize(s,arr){
24+
return [...s].map((x,i)=>arr.includes(i)?x.toUpperCase():x).join('')
25+
};

0 commit comments

Comments
 (0)