Skip to content

Instantly share code, notes, and snippets.

@PuzoLiang
Created May 16, 2019 03:18
Show Gist options
  • Save PuzoLiang/500e59a9435f9b79094a444d3898c5b2 to your computer and use it in GitHub Desktop.
Save PuzoLiang/500e59a9435f9b79094a444d3898c5b2 to your computer and use it in GitHub Desktop.
面试题
/**
* @description 实现一个函数,在任意多个不定长度的数组中,查找出重复的元素,并且记数重复的次数。
* @param array pointer...
* @returns number of virus: Number
* @author LIANGJINGTANG
* @time 2019年05月15日21:32
*/
/**
* @example 例如:var arrayA = [1, 2, 3];
var arrayB = [1, 4, 7];
var arrayC = [2, 8, 1];
期待打印结果:1: 出现 3 次,2: 出现 1 次 (不用关心顺序)
*
*/
// 整合数组指针
findElement([1,2,3],[1,4,7],[2,8,1]);
function findElement(...args) {
let newArray = [];
for (let i = 0; i < args.length; i++) {
newArray = newArray.concat(args[i]);
}
maxN(newArray);
}
// 传入封装好的数组算法
function maxN(arr) {
arr.sort(function (a, b) {
return a - b;
});
var arr_num = new Array();
var arr_time = new Array();
var num = arr[0];
arr_num[0] = arr[0];
arr_time[0] = 1;
var k = 0;
var change_num;
var change_time;
for (let i = 1; i < arr.length; i++) {
if (arr[i] != num) {
k++;
num = arr[i];
arr_num[k] = arr[i];
arr_time[k] = 1;
}
else {
arr_time[k] = arr_time[k] + 1;
}
}
for (let i = 0; i < arr_time.length; i++) {
for (let j = i + 1; j < arr_time.length; j++) {
if (arr_time[i] < arr_time[j]) {
change_time = arr_time[i];
arr_time[i] = arr_time[j];
arr_time[j] = change_time;
change_num = arr_num[i];
arr_num[i] = arr_num[j];
arr_num[j] = change_num;
}
}
}
var arr_N1 = new Array();
arr_N1 = arr_time.sort(function (a, b) {
return b - a;
});
var arr_N = new Array();
arr_N[0] = arr_N1[0];
k = 0;
for (let i = 0; i < arr_N1.length; i++) {
if (arr_N[k] != arr_N1[i]) {
k++;
arr_N[k] = arr_N1[i];
}
}
// 整理出重复元素和重复次数
for (let i = 0; i < arr_time.length; i++) {
if(arr_time[i] !== 1) {
console.log(`${arr_num[i]}元素出现了${arr_time[i]}次`);
}
}
}
/**
* @description 有一种病毒 ,它每天分裂出一个子病毒 。每个子病毒从第四天开始,每天也分裂一个病毒。请你计算第n天是共有多少病毒.
* @param day: Number
* @returns number of virus: Number
* @author LIANGJINGTANG
* @time 2019年05月15日21:32
*/
function fission(day) {
let countNumber = 0;
for(let i = 1; i <= day -3; i++) {
countNumber += i;
}
return countNumber + day + 1;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>搜索引擎输入框</title>
<style>
inpu {
display: block;
clear: both;
}
#selector {
display: none;
position: relative;
left: -40px;
top: -16px;
width: 546px;
}
#selector ul li a {
display: block;
padding: 5px;
border: 1px solid black;
}
#selector ul {
list-style-type: none;
list-style-position: inside;
}
#selector ul li a:link {
text-decoration: none;
}
#selector ul li a:hover {
background-color: lightsteelblue;
cursor: pointer;
}
#selector ul li a:visited {
color: black;
}
</style>
</head>
<body>
<input placeholder="请输入关键词并按回车搜索" style="width: 500px;" id="search" />
<div id="selector" class="js-select search-component"
data-select='{"select":"","options":["Apple","Microsoft","Google","Amazon","Twitter","Intel"]}'>
</div>
<iframe id="searchSection" style="border: 2px solid" src="https://www.baidu.com/s?ie=UTF-8&wd=" frameborder="0"
width="500" height="300"></iframe>
</body>
<script>
// html渲染完成之后执行
window.onload = function () {
const search = document.querySelector("#search");
const selector = document.querySelector("#selector");
const searchSection = document.querySelector("#searchSection");
const selectorItems = JSON.parse(selector['dataset'].select);
const ul = document.createElement("ul");
// 动态创建备选DOM
for (let i = 0; i < selectorItems.options.length; i++) {
const textNode = document.createTextNode(selectorItems.options[i]);
const li = document.createElement("li");
const a = document.createElement("a");
a.appendChild(textNode);
li.appendChild(a);
ul.appendChild(li);
}
// 获取焦点展示搜索框
search.addEventListener("focus", () => {
selector.appendChild(ul);
selector.style.display = "block";
const Alinks = document.getElementsByTagName("a");
for (let i = 0; i < Alinks.length; i++) {
Alinks[i].addEventListener("click", (e) => {
const targetValue = e.target.childNodes[0].nodeValue;
search.value = targetValue;
searchSection.setAttribute("src", `https://www.baidu.com/s?ie=UTF-8&wd=${targetValue}`);
})
}
});
// 监听回车事件
search.addEventListener("keyup", (event) => {
if (event.keyCode == "13" && search.value === '') {
const Alinks = document.getElementsByTagName("a");
const targetValue = Alinks[0].childNodes[0].nodeValue;
search.value = targetValue;
searchSection.setAttribute("src", `https://www.baidu.com/s?ie=UTF-8&wd=${targetValue}`);
} else if (event.keyCode == "13") {
searchSection.setAttribute("src", `https://www.baidu.com/s?ie=UTF-8&wd=${search.value}`);
} else {
const targetValue = search.value;
const Alinks = document.getElementsByTagName("li");
console.log(`targetValue is ${targetValue}`);
for (let i = 0; i < Alinks.length; i++) {
if (!(targetValue[0] === Alinks[i].firstChild.childNodes[0].nodeValue[0].toLocaleLowerCase())) {
// console.log(`das`);
ul.removeChild(Alinks[i]);
} else {
}
}
}
});
// 失去焦点0.5秒延迟关闭展示框
search.addEventListener("blur", () => {
setTimeout(() => {
selector.style.display = "none";
}, 500);
});
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment