본문 바로가기

Computer/HTML, CSS, Javascript

[Javascript] 과제: 기본적인 연산

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Math

 

Test your skills: Math - Learn web development | MDN

The aim of the tests on this page is to assess whether you've understood the Basic math in JavaScript — numbers and operators article.

developer.mozilla.org

 

Math 1

산술 연산자를 쓸 수 있는지 알기 위해 나온 문제입니다.

  1. Number를 담고 있는 네 개의 변수를 생성합니다. 변수 이름을 잘 짓습니다.
  2. 처음 두개의 변수를 더하고 또 다른 변수에 값을 저장합니다.
  3. 세번째의 변수에 네번째 변수를 빼고 다른 변수에 그 값을 저장합니다.
  4. 2번과 3번의 결과를 곱해서 finalResult 변수에 저장합니다.
  5. finalResult를 짝수 수인지 확인하고 evenOddResult에 저장합니다. (0은 짝수, 1은 홀수)
let finalResult;

let evenOddResult;

// Add your code here
// 1번의 과제에 해당하는 코드입니다.
let firstNum = 5;
let secondNum = 3;
let thirdNum = 2;
let fourthNum = 1;

let fifthNum = firstNum + secondNum; // 2번 과제
let sixthNum = thirdNum - fourthNum; // 3번 과제
finalResult = fifthNum * sixthNum;

evenOddResult = finalResult % 2; // 5번 과제

// Don't edit the code below here!

section.innerHTML = ' ';
const para1 = document.createElement('p');
const finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
const para2 = document.createElement('p');
const evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.';
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);

 

결과는 다음과 같습니다.

Is the finalResult 48? No, it is 8

The final result is even!

Math 2

이 문제는 두번의 연산의 결과를 변수 result와 result2에 저장합니다. 연산 결과를 곱해야 하고 십진수로 나타내야 합니다.

  1. result 변수에 result와 result2를 곱한 값을 저장합니다. (약식으로 저장합니다.)
  2. result를 십진로 바꾼 뒤에 변수 finalResult에 저장합니다.
  3. finalResult의 데이터 타입을 확인합니다. string이라면 number로 바꾸고 finalNumber에 저장합니다.

최종 결과 즉 finalNumber에는 4633.33의 값이 있어야 합니다.

// Final result should be 4633.33
// Add/update your code here

let result = 7 + 13 / 9 + 7;
let result2 = 100 / 2 * 6;

result *= result2; // 1번 과제
finalResult = result; /* 2번 과제 이미 십진수이기에 그냥 저장합니다. */

// 3번 과제 조건문과 typeof 함수를 활용해 숫자로 저장할 수 있도록 구현합니다.
if (typeof(finalResult) === String) {
finalNumber = Number(finalResult);
} else {
finalNumber = finalResult;
}

// Don't edit the code below here!

section.innerHTML = ' ';
const para1 = document.createElement('p');
para1.textContent = `Your finalResult is ${ finalResult }`;
const para2 = document.createElement('p');
const finalNumberCheck = typeof finalNumber === 'number' ? 'finalNumber is a number type. Well done!' : `Ooops! finalNumber is not a number.`;
para2.textContent = finalNumberCheck;

section.appendChild(para1);
section.appendChild(para2);

 

결과는 다음과 같습니다.

Your finalResult is 4633.333333333333

finalNumber is a number type. Well done!

Math 3

마지막 문제는 몇개의 테스트를 작성하는 문제입니다. 세개의 그룹이 있고 각각의 그룹은 하나의 명령과 두 개의 변수를 가지고 있습니다. 각각의 그룹에서 명령을 증명하거나 반증하는 테스트를 만드세요. 그 결과를 차례대로 weightComparison, heightComparison, pwdMatch 변수에 저장합니다.

// Statement 1: The elephant weighs less than the mouse
// 명제 1: 코끼리의 무게는 쥐의 무게보다 가볍다
const eleWeight = 1000;
const mouseWeight = 2;
let weightComparison = eleWeight < mouseWeight

// Statement 2: The Ostrich is taller than the duck
// 명제 2: 타조는 오리보다 크다
const ostrichHeight = 2;
const duckHeight = 0.3;
let heightComparison = ostrichHeight > duckHeight;

// Statement 3: The two passwords match
// 명제 3: 두 개의 비밀번호는 일치한다.
const pwd1 = 'stromboli';
const pwd2 = 'stROmBoLi';
let pwdMatch = pwd1 === pwd2;

// Add your code here

// Don't edit the code below here!

section.innerHTML = ' ';
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const para3 = document.createElement('p');

const weightTest = weightComparison ? 'True — elephants weigh less than mice!?' : 'False — of course an elephant is heavier than a mouse!';
const heightTest = heightComparison ? 'True — an ostrich is indeed taller than a duck!' : 'False — apparently a duck is taller than an ostrich!?';
const pwdTest = pwdMatch ? 'True — the passwords match.' : 'False — the passwords do not match; please check them';

para1.textContent = weightTest;
section.appendChild(para1);
para2.textContent = heightTest;
section.appendChild(para2);
para3.textContent = pwdTest;
section.appendChild(para3);

 

결과는 다음과 같습니다.

False — of course an elephant is heavier than a mouse!

True — an ostrich is indeed taller than a duck!

False — the passwords do not match; please check them

 

 

오류 있으면 댓글 남겨주세요. 감사합니다!

728x90

'Computer > HTML, CSS, Javascript' 카테고리의 다른 글

[Javascript] 과제: 배열  (1) 2024.10.25
[Javascript] 과제: Silly story generator  (0) 2024.10.25
[Javascript] 과제: 변수  (1) 2024.10.24
[CSS] 과제: 레이아웃  (2) 2024.10.24
[CSS] 과제: 미디어 쿼리  (4) 2024.10.23