DAY 6. 반복문 복습

냐하호후·2021년 6월 22일
0

hasRepeatedCharacter

function hasRepeatedCharacter(str) {
for(let i= 0;i < str.length; i++){
  for(let j= i+1; j < str.length; j++){
  if(str[i] === str[j]){
  return true ; 
   }
  }
 }
 return false ;
}
//str에서 중복된 letter찾기
//str[i]를 사용해서 글자를 하나 골라낸다
//일치하는 letter가 있다면 true
//중복하는 letter가 없다면 false

let j =i+1 을 못해서 헤맸다. 예를 들어 출력값 abcdef에서 중복되는 letter를 찾을 때 i=0으로 a를 본다. a와 같은 letter가 있나 j에서 찾게되는데 a뒤에 있는 b,c,d순서로 뒤의 letter들을 쭉 훑어본다 그래서 let j = i + 1이 된다.

makeMarginalString

//출력 값
let output = makeMarginalString('abc');
console.log(output); // --> 'aababc'
output = makeMarginalString('flower');
console.log(output); // --> 'fflfloflowfloweflower'
function makeMarginalString(str) {
let result = ''
for (let i= 0; i < str.length; i++) {
  for (let j= 0; j <= i ; j= j+1){
    result = result + str[j]
  }
 }
  return result ;
}
//letter의 첫 글자 > 첫글자 + 두번째글자 > 첫번째글자 + 두번째글자 + 세번째글자 + ... n

j <= i인 이유를 몰랐다. 사실 오늘이 되어서야 진정한 console.log를 쓰는 법을 배운것 같다 console.log를 사용하면 잘 구할 수 있을 것 같다

profile
DONE is better than PERFECT

0개의 댓글