博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeWars in action(2014-05-06)
阅读量:5874 次
发布时间:2019-06-19

本文共 5322 字,大约阅读时间需要 17 分钟。

hot3.png

一、

Description:

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.

my solution:

function noOdds(values) {    var arr = [];    values.filter(function (val) {        if (val % 2 === 0) {            arr.push(val);        }    });    console.log(arr);    return arr;}

solution from web:

function no_odds( values ){  // Return all non-odd values  return values.filter(function(val){return val%2===0})}

二、

Description:

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.

example:

getMissingElement( [0, 5, 1, 3, 2, 9, 7, 6, 4] ) // returns 8 getMissingElement( [9, 2, 4, 5, 7, 0, 8, 6, 1] ) // returns 3

mysolution:

//my solutionfunction 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;}

solution from web:

//外国佬的解决方案function getMissingElement(superImportantArray) {    return 45 - superImportantArray.reduce(function (a, b) {        return a + b;    });}

三、

Description:

Complete the keysAndValues function so that it takes in an object and returns the keys and values as separate arrays.

Example:

keysAndValues({a: 1, b: 2, c: 3}) // should return [['a', 'b', 'c'], [1, 2, 3]]

mysolution:

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});

solution from web:

function keysAndValues(data){  var keys = Object.getOwnPropertyNames(data),      vals = keys.map(function (key) { return data[key]; });  return [keys, vals];}

四、

Description:

Happy Holidays fellow Code Warriors!

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!

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

Person Object Examples:

{ name: 'Warrior reading this kata', wasNice: true } { name: 'xDranik', wasNice: false }

Test Example:

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' ]

my solution:

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;}

solution from web:

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;  });}

五、

Description:

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:

my solution:

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(",");};

转载于:https://my.oschina.net/fengshuzi/blog/261147

你可能感兴趣的文章
Zedboard安装桌面系统ubuntu及opencv(2)
查看>>
函数声明优先级高于变量赋值
查看>>
20151217jqueryUI--自动补全工具
查看>>
链接脚本与重定位
查看>>
Hibernate 框架基本知识
查看>>
keystone nova v2 python
查看>>
VMware虚拟机Bridged(桥接模式)
查看>>
hdu4747 线段树区间修改值,区间查询和及最大值即最大值位置
查看>>
Python 字符串、列表、字典 操作方法大全 & 正则re
查看>>
Vue.js 介绍及其脚手架工具搭建
查看>>
Register code
查看>>
oracle基础入门(二)
查看>>
java 基础知识-数组的7种算法(排序、求和、最值、遍历...)
查看>>
倒要看看你有啥本事
查看>>
bzu-java(三)
查看>>
【初体验】valgrind分析程序性能
查看>>
testlink(以及服务器)问题定位思路
查看>>
Liferay Portal使用MySQL数据库配置
查看>>
个人代码库の模拟QQ振屏功能
查看>>
51Nod:1268 和为K的组合
查看>>