// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
const chunked=[]
for(let element of array){}
const last = chunked[chunked.length - 1];
if(!last || last.length ===size){ chunked.push([element]);
last.push(element)
해줘야함. last변수는 chunkfunction chunk(array, size) {
const chunked = [];
for (let element of array) {
const last = chunked[chunked.length - 1];
if (!last || last.length === size) {
chunked.push([element]);
} else {
last.push(element);
}
}
return chunked;
}
function chunk(array,size){
const chunked=[];
let index=0;
}
그다음 while loop을 작성해주자. index가 기존 배열 길이보다 적은만큼 while문을 돌리고 싶다.
function chunk(array,size){
const chunked=[];
let index=0;
while(index<array.length){
chunked.push(array.slice(index,index+size));
index+=size;
}
return chunked;
}
One string is an anagram of another if it uses the same characters in the same quantity.
// anagrams('rail safety', 'fairy tales') --> True
// anagrams('RAIL! SAFETY!', 'fairy tales') --> True
// anagrams('Hi there', 'Bye there') --> False
1.helper function만들기
function buildCharMap(str){
const charMap={};
for(let char of str.replace(/[^\w]/g,'').toLowerCase()){
charMap[char]=charMap[char]+1 ||1;
}
return charMap;
}
이 함수에 string을 패스한다.
함수 안에 character map을 다룰 빈 객체를 만든다.
string을 iterate하고 그 안에 모든 character들은 character map에 추가될 것이다.
2. string a와 string b를 이용해 character map을 만든다.
function anagrams(stringA,stringB){
const aCharMap= buildCharMap(stringA);
const bCharMap=buildCharMap(stringB);
if(Object.keys(aCharMap).length !==Object.keys(bCharMap).length){
return false;
}
for(let char in aCharMap){
if(aCharMap[char] !==bCharMap[char]){
return false;
}
}
return true;
}
object에서 key 리턴하는 법
const obj={ a:1, b:1, c:1 }; Object.keys(obj);//["a","b","c"] Object.keys(obj).length;//3 (key갯수)
total solution
function anagrams(stringA, stringB) {
const aCharMap = buildCharMap(stringA);
const bCharMap = buildCharMap(stringB);
if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) {
return false;(anagram은 key길이가 서로 같아야 함)
}
for (let char in aCharMap) {
if (aCharMap[char] !== bCharMap[char]) {
return false;
}
}
return true;
}
function buildCharMap(str) {
const charMap = {};
for (let char of str.replace(/[^\w]/g, "").toLowerCase()) {
charMap[char] = charMap[char] + 1 || 1;
}
return charMap;
}
string 안 각 단어들 중에 첫번 째 글자를 대문자로 작성해야 한다.
// capitalize('a short sentence') --> 'A Short Sentence'
// capitalize('a lazy fox') --> 'A Lazy Fox'
// capitalize('look, it is working!') --> 'Look, It Is Working!'
1. empty array를 만들자.
const word=[];
2. split를 사용해보자.
ex) const sentence="Hi there buddy!"
sentence.split(' ')
//["Hi","there","buddy!"]
for (let word of str.split(' ')){
words.push(word[0].toUpperCase()+word.slice(1));
}
return words.join(' ');
total solution
function capitalize(str) {
const words = [];
for (let word of str.split(" ")) {
words.push(word[0].toUpperCase() + word.slice(1));
}
return words.join(" ");
}