1. Given an array, find the integer that appears an odd number of times. There will always be only one integer that appears an odd number of times.
var oddFun = (arr) => arr.reduce((a, b) => a ^ b)
2. 将字符串中除后四位其余替换成#
example: '111111' => '##1111'
var fun = (() => {
var star = '#'
for(var i = 1; i < str.length; i++ ){
star+='#'
}
return star + str.substring(str.length-4)
})(str)
answer = str.length < 4 ? str : fun
(* 题目找不到只有自己方法)
3.找到字母对应26个英文字母中的位置
var str = 'this is sunset'
function fun(str) {
return str.toUpperCase().replace(/[^A-Z]/g, '').join('').map(item => item.charCodeAt() - 64).join(' ')
}
4.Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contain any char
function fun(str) {
var str = str.toUpperCase()
var xNum = str.split('X').length
var oNum = str.split('O').length
return xNum === oNum
}
function fun(str) {
return str.replace(/x/gi, '').length === str.replace(/o/gi, '').length
}
function fun(str) {
var x = str.match(/x/gi)
var o = str.match(/o/gi)
return (x && x.length) === (o && o.length)
}
5.Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number (找到一个数串中和其他不一样的数的索引)
5.Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number (找到一个数串中和其他不一样的数的索引)
function fun(number) {
var numberArr = number.split(' ').map(item => parseInt(item))
var oddArr = numberArr.filter(item => item % 2 === 1)
var evenArr = numberArr.filter(item => item % 2 === 0)
return oddArr.length > evenArr.length ? numberArr.indexOf(evenArr[0]) + 1 : numberArr.indexOf(oddArr[0]) + 1
}