CodeWars Weekly Challenge 1

Codewars Weekly Challenge 1

1 问题描述

Create a function which answers the question “Are you playing banjo?”.If your name starts with the letter “R” or lower case “r”, you are playing banjo!The function takes a name as its only argument, and returns one of the following strings:
name + " plays banjo"
name + " does not play banjo"

  • mine
function areYouPlayingBanjo(name) {
  if(name.substring(0,1)=='R'||name.subString(0,1)=='r'){
  	name+=" plays banjo";
  }else{
  	name+=" does not play banjo";
  }
  return name;
}
  • clever
function areYouPlayingBanjo(name) {
  return name + (name[0].toLowerCase() == 'r' ? ' plays' : ' does not play') + " banjo";
}
2 问题描述

Definition
A number is called Automorphic number if and only if its square ends in the same digits as the number itself.
Task
Given a number determine if it Automorphic or not .

  • mine
function automorphic(n){
  var length1=n.toString().length;
  var length2=(n*n).toString().length;
  return (n*n).toString().substring(length2-length1,length2)==n.toString()?"Automorphic":"Not!!";
}
  • clever
const automorphic = n => String(n*n).endsWith(String(n)) ? "Automorphic" : "Not!!" ;
3 问题描述

Description:
Implement the method map, which accepts a linked list (head) and a mapping function, and returns a new linked list (head) where every element is the result of applying the given mapping method to each element of the original list.

Make sure you do not modify the original list!

For example: Given the list: 1 -> 2 -> 3, and the mapping function x => x * 2, map should return 2 -> 4 -> 6

The linked list is defined as follows:
function Node(data, next = null) {
this.data = data;
this.next = next;
}
Note: the list may be null and can hold any type of value.

  • Answer
function map(head, f) {
  return !head ? null : new Node(f(head.data), map(head.next, f));
}
4问题描述

Description:
Given the string representations of two integers, return the string representation of the sum of those integers.

For example:

sumStrings(‘1’,‘2’) // => ‘3’

  • Answer
function sumStrings(a,b) { 
  var res='',c=0;
  a=a.split('');
  b=b.split('');
  while(a.length||b.length||c){//pop() 方法用于删除并返回数组的最后一个元素
   c+=~~a.pop()+~~b.pop();
   res=c%10+res;
   c=c>9;//是否进位
  }
  return res.replace(/^0+/,'');//替代开头的多个0,比如sumStrings('00103', '08567') - Expected: '8670', instead got: '08670'
}
5 问题描述

A pangram is a sentence that contains every single letter of the alphabet at least once. For example, the sentence “The quick brown fox jumps over the lazy dog” is a pangram, because it uses the letters A-Z at least once (case is irrelevant).

Given a string, detect whether or not it is a pangram. Return True if it is, False if not. Ignore numbers and punctuation.

  • mine
function isPangram(string){
  var character;
  var flag=0;
  for(i=0;i<26;i++){
     character=String.fromCharCode(65+i);
     if((string.indexOf(character.toLowerCase())==-1)&&(string.indexOf(character)==-1)){
        flag=1;
        break;
     }
  }
  return flag==0?true:false;
}
  • clever
function isPangram(string){
  string = string.toLowerCase();
  return "abcdefghijklmnopqrstuvwxyz".split("").every(function(x){
    return string.indexOf(x) !== -1;
  });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值