3. 배열의 반복 1 , 2
2020. 7. 4. 23:19ㆍRetrospection/Sprint
배열의 반복
1.
Write a function called "computeSumOfAllElements".
Given an array of numbers, "computeSumOfAllElements" returns the sum of all the elements in the given array.
function computeSumOfAllElements(arr) {
//sum = 0 을 선언한다
let sum = 0;
//for loop 로 i 값을 만든다 . 길이는 arrlength 보다 작다
for(i =0; i < arr.length ; i++){
sum = sum + arr[i];
}
//sum = sum + arr[i]
//for loop 끝내고
//return sum
return sum;
}
2.
Write a function called "getAllLetters".
Given a word, "getAllLetters" returns an array containing every character in the word.
Notes:
- If given an empty string, it should return an empty array.
- 반드시 for 문을 이용해야 합니다.
function getAllLetters(str) {
//result 는 빈배열 을 선언하고
// if str === ''; 이면 빈배열 반환한다
//else
// i 가 0이고 str.length 보다 작고 1씩 증가할때
// result 에다가 str[i]를 push 해준다.
//return result
let result = [];
if(str === ''){
return [];
}else {
for(i=0; i < str.length ; i++){
result.push(str[i]);
}
return result;
}
}
푸는데 되게 오래걸렸다.
별거아닌데 왜이렇게 헷갈리는지
result.push(str[i])
라고 할지
result = result + str[i]; 라고 했는데 테스트 통과가 안됐음. 저런식으로 하면 안되나보다.
'Retrospection > Sprint' 카테고리의 다른 글
4. 객체 1~ 3 (0) | 2020.07.07 |
---|---|
윈도우 사이즈 (0) | 2020.07.06 |
3. 반복문 1~3 (0) | 2020.07.04 |
배열기초 /타입 1 , 배열기초 1-3 (0) | 2020.07.04 |
2. 타입 1 ~3 (0) | 2020.07.02 |