2014-12-02 2 views
-2

여기 내 도전 :자바 스크립트의 경우 비교/관계 연산자

  • Complete the canIGet function. This function should:
    • take two arguments:
      • 'item' should represent what the user wants to buy
      • 'money' should represent how many dollars a user has
      • return 'true' if a user can afford a given item according to the price chart below, false otherwise:
        • 'MacBook Air' - $999
        • 'MacBook Pro' - $1299
        • 'Mac Pro' - $2499
        • 'Apple Sticker' - $1
      • return 'false' if the 'item' is not in the above list apple products

이 (오히려/거짓 내가 콘솔에 로그인하고 진정한 돌아보다)

내가 난 무엇에 대한 설명을 부탁드립니다 잘못하고있다.

내 코드 :

var canIGet = function(item, money){ 
    if ((money >= 2499) && (item == "Mac Pro" || "Macbook Pro" || "Macbook Air" || "Apple sticker")){ 
     console.log("You can afford a Mac Pro"); 
    } 
    else if ((2499 > money) && (money >= 1299) && (item == "Macbook Pro" || "Macbook Air" || "Apple sticker")){ 
     console.log("You can afford a Macbook Pro"); 
    } 
    else if ((1299 > money) && (money >= 999) && (item == "Macbook Air" || "Apple sticker")){ 
     console.log("You can afford a Macbook Air"); 
    } 
    else if ((999 > money) && (money >= 1) && (item == "Apple sticker")){ 
     console.log("You can afford a Apple sticker"); 
    } else { 
     console.log("Get a job!"); 
    } 
}; 
canIGet("Mac Pro", 1500); 
+0

로 작성해야

(item == true || true || true || true) (item == true) ("Sticker" == true) true 

로 실행/분석의 '내가 잘못 뭘하는지에 대한 설명을 부탁드립니다. "* 무엇을 만드는 당신이 뭔가 잘못하고 있다고 생각 ? 코드에 문제가 있습니까? 이게 뭐야? –

+0

답변을 확인하려면 모카 테스트가 필요합니다. 그의 답변에 마크 B가 제안한대로 고칠 때조차도 여전히 문제가 있습니다. – Tobber

+0

* "답변을 확인하려면 모카 테스트가 있습니다."* 뭐라 구요? "우리"는 누구이며 시험은 어디에 있습니까? * "문제가 아직도있다"* 당신이 우리에게 그들에 대해 말하지 않으면 우리는 당신을 도울 수 없다. –

답변

1

기본 자바 스크립트 (대부분의 다른 언어) :

(item == "Mac Pro" || "Macbook Pro" || "Macbook Air" || "Apple sticker")){ 

그 문자열의 각각에 대한 item을 테스트하지 않습니다. 당신은 문자열의 부울 OR을 수행하고 그 결과를 OR 아이템과 비교합니다.

당신이 *

if (item == 'Mac Pro') || (item == 'Macbook Pro') || etc... 
+0

아 .. 고마워. – Tobber