Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[자동차 경주 게임] 김용래 미션 제출합니다. #116

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 사용자가 잘못된 입력 값을 작성하면 경고하기
- isValidate()를 이용하여 사용자의 입력
  값을 검증
- 자동차의 이름이 5자를 초과하는 경우
- 입력값이 1보다 작을 경우
  • Loading branch information
usageness committed Dec 6, 2021
commit 07cef726da48fe5ab5234a37fdf7bb1d8e727a09
8 changes: 7 additions & 1 deletion src/car/CarList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Car from "./Car.js";
import isMovable from "../utils/isMovable.js";
import makeResultElement from "../utils/makeResultElement.js";
import isValidate from "../utils/isValidate.js";

class CarList {
constructor() {
Expand All @@ -13,7 +14,12 @@ class CarList {

init(carNameArray) {
for(let i = 0; i<carNameArray.length; i++) {
this.push(new Car(carNameArray[i]));
if(isValidate("name", carNameArray[i])) this.push(new Car(carNameArray[i]));
else {
this.array = [];
alert("잘못된 이름입니다.");
return;
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CarList from "./car/CarList.js";
import isValidate from "./utils/isValidate.js";

const formTag = document.querySelectorAll("form");
const carNameButton = document.getElementById("car-names-submit");
Expand All @@ -22,6 +23,9 @@ carNameButton.addEventListener('click', event => {
racingCountButton.addEventListener('click', event => {
const racingCountInput = document.getElementById("racing-count-input").value;

carList.game(racingCountInput);
console.log(carList);
if(!isValidate("count", racingCountInput)) alert("잘못된 숫자를 입력하셨습니다.");
else {
carList.game(racingCountInput);
console.log(carList);
}
});
14 changes: 14 additions & 0 deletions src/utils/isValidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function isValidate(type, input) {
if(type === "name") return !isOverFiveSyllable(input);
else return isPositive(input);
}

function isOverFiveSyllable(input) {
return input.length > 5;
}

function isPositive(input) {
return input >= 1;
}

export default isValidate;