I have seen a lot of incorrect answers to a Javascript interview question. Can you check whether my answers are correct?

Source: Internet
Author: User
Tags switch case

Question: Create a method. if the input number is less than or equal to 100, return a. if the input number is greater than 100, return B. if the input number is less than, return if else, ||? &, Switch case

The answer is as follows:

Function f (v, a, B ){
V = Math. min (Math. ceil (v), 101); // obtain a number smaller than 101. If it is greater than 101, 101 is returned.
V = Math. floor (v/101); //> 100 returns 1, <= 100 returns <= 0
V = Math. max (v, 0); // remove the negative number.
Return {
"0":,
"1": B
} [V];
}
Document. writeln (f (100, 1, 2); // equals to 100, return 1
Document. writeln (f (300, "a", "B"); // greater than 100, return B
Document. writeln (f (101, "+", "-"); // greater than 100, return-
Document. writeln (f (-101, "I", "j"); // if it is less than 100 and is a negative number, return I
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.