Sharing two-way millet company JavaScript closed-envelope test questions and solution

Source: Internet
Author: User

Millet company JavaScript closed-pack face test

One, please define such a function

The code is as follows Copy Code
function repeat (func, times, wait) {
}
This function can return a new function, such as using the
var repeatedfun = repeat (alert, 10, 5000)
Call this repeatedfun ("Hellworld")
Alert 10 times HelloWorld, each interval of 5 seconds



Two, write a function stringconcat, request can

The code is as follows Copy Code
var result1 = Stringconcat ("A", "B") Result1 = "A+b"
var stringconcatwithprefix = Stringconcat.prefix ("Hellworld");
var result2 = Stringconcatwithprefix ("A", "B") result2 = "Hellworld+a+b"


Solution

These two questions, the test is closure, nonsense not much said, directly on the code.

The code is as follows Copy Code
/**
* First question
* @param func
* @param times
* @param wait
* @returns {Repeatimpl}
*/
function repeat (func, times, wait) {
No anonymous function is necessary to facilitate debugging
function Repeatimpl () {
var handle,
_arguments = arguments,
i = 0;
Handle = SetInterval (function () {
i = i + 1;
Cancel timer at specified number of times
if (i = = times) {
Clearinterval (handle);
Return
}
Func.apply (null, _arguments);
},wait);
}
return Repeatimpl;
}
Test Cases
var repeatfun = repeat (Alert, 4, 3000);
Repeatfun ("Hellworld");
/**
* Second question
* @returns {string}
*/
function Stringconcat () {
var result = [];
Stringconcat.merge.call (null, result, arguments);
Return Result.join ("+");
}
Stringconcat.prefix = function () {
var _arguments = [],
_this = this;
_this.merge.call (null, _arguments, arguments);
return function () {
var _args = _arguments.slice (0);
_this.merge.call (null, _args, arguments);
return _this.apply (null, _args);
};
};
Stringconcat.merge = function (array, arraylike) {
var i = 0;
for (i = 0; i < arraylike.length; i++) {
Array.push (Arraylike[i]);
}
}
Test Cases
var result1 = Stringconcat ("A", "B"); RESULT1 = "A+b"
var result3 = Stringconcat ("C", "D"); RESULT1 = "A+b"
var stringconcatwithprefix = Stringconcat.prefix ("Hellworld");
var stringconcatWithPrefix1 = Stringconcat.prefix ("Hellworld1");
var result2 = Stringconcatwithprefix ("A", "B"); RESULT2 = "Hellworld+a+b"
var result4 = stringconcatWithPrefix1 ("C", "D"); RESULT2 = "Hellworld+a+b"
alert (RESULT1);
alert (RESULT2);
alert (RESULT3);
alert (RESULT4);

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.