if๋ฌธ ์ค‘์ฒฉ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.


๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ
๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ํ•„์š”๋กœ ํ•˜์˜€๋‹ค.

1. ๊ธธ์ด๊ฐ€ 6๊ธ€์ž ์ด์ƒ์ผ ๊ฒƒ.
2. ๊ณต๋ฐฑ์ด ์—†์„ ๊ฒƒ.

const password = prompt("please enter a new password");

๋ณ€์ˆ˜๋กœ password๋ฅผ ์ง€์ •ํ•œ ํ›„ ํ”„๋กฌํ”„ํŠธ์ฐฝ์— ๋„์› ๋‹ค.

const password = prompt("please enter a new password");

// Password must be 6+ characters
if (password.length >= 6) {
    // Password cannot include space 
    if (password.indexOf(' ') === -1) {
        console.log("Valid Password!");
    } else {
        console.log("Password cannot contain spaces!")
    }
} else {
    console.log("PASSWORD TOO SHORT! Must be 6+ characters")
}

indexOf(' ')
-1
๊ณต๋ฐฑ์ฒ˜๋Ÿผ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์ฐพ๋Š” ๊ฒฝ์šฐ -1 ์ถœ๋ ฅ

    if (password.indexOf(' ') === -1) {
        console.log("Valid Password!");
์ฆ‰ (' ') ๊ด„ํ˜ธ ์•ˆ์˜ ๊ณต๋ฐฑ์ด -1 ์ด๋‹ค => ๊ณต๋ฐฑ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
-1 -> ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ ์ถœ๋ ฅ
๋”ฐ๋ผ์„œ ์œ ํšจํ•œ password๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ.
 


"hello".indexOf('h')
0 ์ด ์ถœ๋ ฅ๋จ.
์กด์žฌํ•˜๋Š” ๊ฒƒ


'javaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ARRAY METHODS ์™€ ์—ฌ๋Ÿฌ METHODS  (0) 2022.09.20
if/else/else-if ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด๋ณด์ž.  (0) 2022.09.19

+ Recent posts