function isPrime(n) { if (n === 1) { // Special case for 1 return false; } else { // Go through all numbers from 2 up until n/2 for (let i = 2; i <= n / 2; i += 1) { if (n % i === 0) { // We found a number i such as i * x = n so the number is not prime return false; } } // If we got to this point then we know the number is prime return true; } } let num = 45; console.info("Is " + num + " a prime?: " + isPrime(num)); num = 37; console.info("Is " + num + " a prime?: " + isPrime(num)); num = 73; console.info("Is " + num + " a prime?: " + isPrime(num));
functionprintChildrenContainingClass(rootElement,className){if(rootElement){// Iterate over the childNodeslist.childNodes.forEach((node)=>{// If a childnode contains a className print the nodeif(node.classList.contains(className)){console.info(node);}});}}printChildrenContainingClass(document.querySelector('#list'),'odd'));
4. 编写一个函数,接受一个 DOM 元素和一个字符串作为参数,并打印该元素的父节点中是否有包含该字符串的类。当没有父元素时,该函数应该停止。
functionprintParentsContainingClass(childElement,className){if(childElement){// Iterate over the parentNodesletparentNode=childElement.parentNode;while(parentNode!==null){// If a parentNode contains a className print the nodeif(parentNode.classList&&parentNode.classList.contains(className)){console.info(parentNode);}// Go upparentNode=parentNode.parentNode;}}}printParentsContainingClass(document.getElementById('start'),'sidebar');
functiononListElementClicked(event){if(event.target&&event.target.nodeName=="LI"){// List item found. Alert the innerTextalert(event.target.innerText+" was clicked");}}letlist=document.getElementById('list-start');if(list){list.addEventListener('click',onListElementClicked);}
6. 编写一个函数检查给定的字符串是否是回文。
根据定义,如果一个字符串倒着读,其内容相同,则该字符串为回文。例如,以下字符串就是回文:
“aba”、“assissa”
但是以下字符串不是回文:
“abc”、“阿西比萨”
我们可以使用 for 循环遍历两个索引来检查字符串是否为回文。第一个索引从字符串的开头开始,第二个索引从结尾开始并向开头移动。如果在任意时刻,S[i] !== S[j] 处的字符不匹配,则说明该字符串不是回文。循环到字符串中间时停止。
代码要点如下:
function isPalindrome(inputStr) { let lo = 0; let hi = inputStr.length; let mid = Math.floor((lo + hi) / 2); // Check until the mid element for (let i = 0, j = hi-1; i < mid; i += 1, j -= 1) { if (inputStr[i] !== inputStr[j]) { return false; } } // If we got in here then we know that the string is palindrome return true; } console.info(isPalindrome("ab")) console.info(isPalindrome("a")) console.info(isPalindrome("aba")) console.info(isPalindrome("abc")) console.info(isPalindrome("abba"))
// Node holds the data and a reference to the next node function LinkListNode(data) { this.data = data; this.next = null; } // Linked list Basic Structure function LinkedList() { this.len = 0; this.head = null; } // Operations LinkedList.prototype.push = function(item) { if (item) { let node = new LinkListNode(item); // if the current head is null we set item as head and update the length if (!this.head) { this.head = node; this.len += 1; return node; } // Otherwise we follow the next links until we reach the end of the list let currentNode = this.head; while (currentNode.next !== null) { currentNode = currentNode.next; } // If we got here then we have reached the end. currentNode points to the last element currentNode.next = node; this.len += 1; return node; } }; LinkedList.prototype.head = function() { return this.head; }; LinkedList.prototype.length = function() { return this.len; }; let list = new LinkedList(); list.push(1); list.push(2); list.push(3); list.push(4); console.info('List is: '); let currentNode = list.head while(currentNode !== null) { console.info(currentNode.data); currentNode = currentNode.next; }