HTML, number , function

jiin lee·2024년 1월 3일
0

frontend-javascript이론

목록 보기
9/15

the structure of a web page
the content of web pages

are primary containers in HTML document structure

HTML uses markup symbols or codes inserted in a file intended for display on a browser page. The web browser reads the HTML and renders it into visible or audible web pages.

스크립트에서 사용하는 모든 정보는 데이터 타입을 갖습니다.
이 타입은
1)데이터를 메모리에 저장하는 방법과
2)해당 데이터에 어떤 작업을 적용할지를 설명합니다.

// Importing library
const readline = require('readline');

// The following function will compute the sum of digits of a number
// Here 'str' is the input string representation of a number
function sumOfDigits(str) {

  let sum=0;
  let intArrayList = [];
    // You need to implement the functionality inside this function
    // Convert the string of digits into an array of numbers
  for (let index = 0; index <str.length; index++) {
  //  intArrayList.push(parseInt(str[index]));
  //  sum+=intArrayList[index];
  sum+=parseInt(str[index]);
    
  }

  
    // Then calculate the sum of numbers in the array
    // Finally, return the sum
  return sum;
}

// The function can be tested as follows
console.log(sumOfDigits('4567'))

실행결과 : 22

JavaScript에서는 다양한 산술 연산을 수행하는 연산자를 제공합니다. 이러한 연산자들은 두 피연산자(연산이 수행되는 객체)에 적용되기 때문에 이를 이진(binary) 산술 연산자라고 합니다.

String(value1) - Number(value2) === Number(value3)
String(value1) + Number(value2) === String(value4)

  1. var str1 = '100'; var num1 = 50; var str2 = '50'; var result = str1 - num1 + str2;
    console.log(result)
    -> 5050
    2.var str1 = '100'; var num1 = 50; var str2 = '50'; var result = str1 + num1 + str2;
    console.log(result)
    -> 1005050

76 / 5
-> 주의 이건 결과 정수가 아니라 실수 나옴

자주 동일한 동작을 스크립트에서 여러 번 반복해야 하는 경우가 있습니다. 사용자가 입력한 다양한 데이터를 요약하거나 온라인 상점의 제품 설명을 팝업으로 표시하는 등의 작업이 여기에 해당합니다. 함수는 코드 중복을 피하고 큰 프로그램을 더 잘 구조화하는 데 도움이 되며, 복잡한 프로그램을 더 작고 관리하기 쉬운 부분으로 나누는 데 도움이 됩니다.

JavaScript에서 함수는 내장 함수 또는 사용자 정의 함수로 나뉩니다. 즉, 프로그래머가 명시적으로 만들 수 있습니다. 이미 내장 함수인 console.log()와 함께 작업했습니다. 이제 사용자 정의 함수를 만드는 방법을 배워보겠습니다.

기본 구문:
함수를 만드는 방법의 구문을 살펴보겠습니다.

function name(parameters) {
// function body
}
함수를 만들려면 function 키워드를 작성하고 이름을 지정한 다음 괄호를 열어야 합니다. 괄호 안에는 프로그램에 전달하려는 데이터, 즉 매개변수를 지정할 수 있습니다. 함수 코드 또는 함수 본문은 중괄호 내에 작성해야 합니다.

함수는 파일 어디서든 호출할 수 있습니다. 함수를 생성하기 전에 호출할 수도 있고 후에 호출할 수도 있습니다.

함수를 생성하기 전에 호출할 수 있는 이 능력은 브라우저에 의해 JS 파일이 처리되는 특징으로, 브라우저는 먼저 전체 코드를 검토하고 모든 함수를 찾은 다음에 코드를 실행하기 시작합니다.

writeMessage(); // Find and book your ideal tour!

function writeMessage() {
   console.log("Find and book your ideal tour!");
}

writeMessage(); // Find and book your ideal tour!
function calculate(operator, num1, num2) {
  switch (operator) {
    case '+':
      return num1 + num2;
    case '-':
      return num1 - num2;
    case '*':
      return num1 * num2;
    case '/':
      return num1 / num2;
    default:
      return 'Invalid operator';
  }
}
  1. calculate('+', 5, 3);
  2. let result = calculate('*', 5, 3);
  3. console.log(calculate('/', 5, 3));
profile
creative engineer

0개의 댓글

관련 채용 정보