๐Ÿ“ŒLanguage/JavaScript

TIL) ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ prompt ํ•จ์ˆ˜, prompt ์‚ฌ์šฉ

hellohailie 2022. 5. 15. 15:31

 

prompt()

 

์ž…๋ ฅ์ฐฝ์˜ ์ œ๋ชฉ ๋ถ€๋ถ„์— ๋ฉ”์‹œ์ง€๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๊ณ , ์ฐฝ ์ž…๋ ฅ ๊ณต๊ฐ„์— ๋ฉ”์‹œ์ง€๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

const ํ–‰์„ฑ = [
  '์ˆ˜์„ฑ',
  '๊ธˆ์„ฑ',
  '์ง€๊ตฌ',
  'ํ™”์„ฑ',
  '๋ชฉ์„ฑ',
  'ํ† ์„ฑ',
  '์ฒœ์™•์„ฑ',
  'ํ•ด์™•์„ฑ',
];

const num = prompt('๋ช‡ ๋ฒˆ์งธ ํ–‰์„ฑ์ด ๊ถ๊ธˆํ•œ๊ฐ€์š”?');

console.log(ํ–‰์„ฑ[num - 1]);

 

์ž…๋ ฅํ•œ๋Œ€๋กœ ์ฝ˜์†”์ฐฝ์— ๋กœ๊ทธ๋œ๋‹ค. 

 

4๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋ฉด ํ™”์„ฑ์ด ์ถœ๋ ฅ๋œ๋‹ค. 

 

 


 

const ํ–‰์„ฑ = [
  '์ˆ˜์„ฑ',
  '๊ธˆ์„ฑ',
  '์ง€๊ตฌ',
  'ํ™”์„ฑ',
  '๋ชฉ์„ฑ',
  'ํ† ์„ฑ',
  '์ฒœ์™•์„ฑ',
  'ํ•ด์™•์„ฑ',
];

const num = prompt('๋ช‡ ๋ฒˆ์งธ ํ–‰์„ฑ์ด ๊ถ๊ธˆํ•œ๊ฐ€์š”?', '9๋ฒˆ์งธ ๋ช…์™•์„ฑ์€ ์ œ์™ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');

console.log(ํ–‰์„ฑ[num - 1]);

 

 

prompt("์œˆ๋„์šฐ์ฐฝ", "์ž…๋ ฅ์ฐฝ");

 

 

prompt ํ•จ์ˆ˜๋Š” ๋ชจ๋“  ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž ์ •๋ณด๋กœ ์ธ์‹ํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž ๋ฐ์ดํ„ฐ๋„ ๋ชจ๋‘ ๋ฌธ์žํ˜• ์ˆซ์ž๋กœ ์ธ์‹๋œ๋‹ค.

 

const str1 = prompt("ํ•œ ์ž๋ฆฌ์ˆ˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.", "");
const str2 = prompt("ํ•œ ์ž๋ฆฌ์ˆ˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.", "");

console.log(str1 + str2);

์ฐจ๋ก€๋Œ€๋กœ 1๊ณผ 2๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 3์ด ์•„๋‹Œ '12'๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. 

 

๋”ฐ๋ผ์„œ ๊ณ„์‚ฐ์„ ํ• ๋•Œ๋Š” Numberํ•จ์ˆ˜๋ฅผ ์จ์„œ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค. 

 

 

 

const str1 = prompt("ํ•œ ์ž๋ฆฌ์ˆ˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.", "");
const str2 = prompt("ํ•œ ์ž๋ฆฌ์ˆ˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.", "");

const num1 = Number(str1);
const num2 = Number(str2);

console.log(num1 + num2);

์ฐจ๋ก€๋Œ€๋กœ 1๊ณผ 2๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 3์ด ์ถœ๋ ฅ๋œ๋‹ค.