알고리즘, 코딩테스트 연습 레포지토리입니다.
icstuckyi / algorithms Goto Github PK
View Code? Open in Web Editor NEW알고리즘, 코딩테스트 연습 레포지토리입니다.
알고리즘, 코딩테스트 연습 레포지토리입니다.
문제 원본 링크
https://school.programmers.co.kr/learn/courses/30/lessons/87389
ec6ac40
의사코드 또는 나의 풀이
function solution(n) {
for (let i = 2; i<n; i++){
// n%i === 1 ? return i : null;
if (n%i === 1) return i;
}
}
문제가 되는 부분
return i;
는 왜 {}로 감싸주지 않아도 if문의 조건식 하에 작동하는가?function solution(n){i};
꼴이 되는 것이 아닌가? return 처리를 한번더 해줘야 하는게 아닌가?문제 원본 링크
https://school.programmers.co.kr/learn/courses/30/lessons/120831
bb7f6a4
의사코드 또는 나의 풀이
function solution(n) {
let a = 0;
for (i=0; i<=n; i+=2) {
a += i;
}
return a;
}
문제가 되는 부분
문제는 없었으나, 제어문 안의 조건문에 으레 작성하는 i에도 키워드(let, const)를 사용해 선언해줘야 안전하다고 한다.
출처: https://poiemaweb.com/js-scope
코드가 바르게 작성되었을 때 기대되는 동작
조건문 안의 i에 let이나 const 키워드를 작성하지 않으면 "암묵적 전역" 으로 동작하게 된다. 마치 var처럼 작동하게 되어 오염될 가능성이 있다.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.