Write a small function that returns the values of an array that are not odd.
All values in the array will be integers. Return the good values in the order they are given.
function noOdds(values) { var arr = []; values.filter(function (val) { if (val % 2 === 0) { arr.push(val); } }); console.log(arr); return arr; }
function no_odds( values ){ // Return all non-odd values return values.filter(function(val){return val%2===0}) }
Fellow code warrior, we need your help! We seem to have lost one of our array elements, and we need your help to retrieve it! Our array, superImportantArray, was supposed to contain all of the integers from 0 to 9 (in no particular order), but one of them seems to be missing.
Write a function called getMissingElement that accepts an array of unique integers between 0 and 9 (inclusive), and returns the missing element.
getMissingElement( [0, 5, 1, 3, 2, 9, 7, 6, 4] ) // returns 8 getMissingElement( [9, 2, 4, 5, 7, 0, 8, 6, 1] ) // returns 3
//my solution function getMissingElement(superImportantArray) { var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var result; var i = 0; for (i = 0; i < arr.length; i++) { var exit = false; for (var j = 0; j < superImportantArray.length; j++) { if (arr[i] === superImportantArray[j]) { exit = true; break; } } if (exit === false) { result = arr[i]; break; } } console.log(result); return result; }
//外国佬的解决方案 function getMissingElement(superImportantArray) { return 45 - superImportantArray.reduce(function (a, b) { return a + b; }); }
Complete the keysAndValues function so that it takes in an object and returns the keys and values as separate arrays.
keysAndValues({a: 1, b: 2, c: 3}) // should return [['a', 'b', 'c'], [1, 2, 3]]
function keysAndValues(data) { var keys = []; var maps = []; for (var index in data) { keys.push(index); maps.push(data[index]); } var datas = [keys, maps]; console.log(datas); return datas; } keysAndValues({a: 1, b: 2, c: 3});
function keysAndValues(data){ var keys = Object.getOwnPropertyNames(data), vals = keys.map(function (key) { return data[key]; }); return [keys, vals]; }
It's almost Christmas! That means Santa's making his list, and checking it twice. Unfortunately, Santa's Javascript and CoffeeScript Elves accidentally mixed the Naughty and Nice list together! Santa needs your help to save Christmas!
Santa needs you to write two functions, getNiceNames and getNaughtyNames. Both of the functions accept an array of objects. getNiceNames returns an array containing only the names of the nice people, and getNaughtyNames returns an array containing only the names of the naughty people. Return an empty array [] if the result from either of the functions contains no names.
The objects in the passed in array will represent people. Each object contains two properties: name and wasNice.
name - The name of the person
wasNice - True if the person was nice this year, false if they were naughty
{ name: 'Warrior reading this kata', wasNice: true } { name: 'xDranik', wasNice: false }
getNiceNames( [ { name: 'Warrior reading this kata', wasNice: true }, { name: 'xDranik', wasNice: false }, { name: 'Santa', wasNice: true } ] ) // => returns [ 'Warrior reading this kata', 'Santa' ] getNaughtyNames( [ { name: 'Warrior reading this kata', wasNice: true }, { name: 'xDranik', wasNice: false }, { name: 'Santa', wasNice: true } ] ) // => returns [ 'xDranik' ]
function getNiceNames(people) { var names = []; for (var index in people) { if (people[index].wasNice === true) { names.push(people[index].name); } } console.log(names); return names; } function getNaughtyNames(people) { var names = []; for (var index in people) { if (people[index].wasNice === false) { names.push(people[index].name); } } console.log(names); return names; }
function getNiceNames(people){ return people.filter(function(e){ return e.wasNice; }).map(function(e){ return e.name; }); } function getNaughtyNames(people){ return people.filter(function(e){ return !e.wasNice; }).map(function(e){ return e.name; }); }
An anagram is the result of rearranging the letters of a word to produce a new word. (Ref wikipedia).
Note: anagrams are case insensitive
Examples
foefet is an anagram of toffee
Buckethead is an anagram of DeathCubeK
The challenge is to write the function isAnagram to return true if the word test is an anagram of the word original and false otherwise. The function prototype is as given below:
var isAnagram = function (test, original) { var tests = test.toLowerCase().split("").sort(); var orgs = original.toLowerCase().split("").sort(); console.log(tests,orgs); return tests.join(",") === orgs.join(","); };