codewars练习记录8 js

【7 kyu】Exes and Ohs

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.

Examples input/output:

XO(“ooxx”) => true
XO(“xooxx”) => false
XO(“ooxXm”) => true
XO(“zpzpzpp”) => true // when no ‘x’ and ‘o’ is present should return true
XO(“zzoo”) => false

翻译:
检查一个字符串是否具有相同数量的“x”和“o”。该方法必须返回布尔值并且不区分大小写。字符串可以包含任何字符。
解一:

function XO(str) {
  var x = 0
  var o = 0
  var arr=str.toLowerCase().split('')
  for(let i=0;i<arr.length;i++){
    if(arr[i] =='x'){
      x++
    }else if(arr[i] == 'o'){
      o++
    }
  }
  return o == x 
}

解二:

function XO(str) {
  let x = str.match(/x/gi);
  let o = str.match(/o/gi);
  return (x && x.length) === (o && o.length);
}

解三:

function XO(str) {
    return str.toLowerCase().split('x').length === str.toLowerCase().split('o').length;
}
【8 kyu】Do you speak “English”?

Given a string of arbitrary length with any ascii characters. Write a function to determine whether the string contains the whole word “English”.

The order of characters is important – a string “abcEnglishdef” is correct but “abcnEglishsef” is not correct.

Upper or lower case letter does not matter – “eNglisH” is also correct.

Return value as boolean values, true for the string to contains “English”, false for it does not.
翻译:
给定任意长度的字符串和任意ascii字符。编写一个函数来确定字符串是否包含整个单词“English”。
字符的顺序很重要——字符串“abcEnglishdef”是正确的,但“abcnEnglishsef”是不正确的。
大写或小写字母并不重要——“eNglisH”也是正确的。
返回值为布尔值,true表示字符串包含“English”,false表示不包含。
解一:

function spEng(sentence) {
  return sentence.toLowerCase().indexOf("english") != -1;
}

解二:

function spEng(sentence){
   return /english/i.test(sentence)
}

解三:

function spEng(sentence){
  return sentence.toLowerCase().includes("english");
}
【8 kyu】Remove First and Last Character Part Two

This is a spin off of [my first kata]

You are given a string containing a sequence of character sequences separated by commas.

Write a function which returns a new string containing the same character sequences except the first and the last ones but this time separated by spaces.

If the input string is empty or the removal of the first and last items would cause the resulting string to be empty, return an empty value (represented as a generic value NULL in the examples below).

Examples

“1,2,3” => “2”
“1,2,3,4” => “2 3”
“1,2,3,4,5” => “2 3 4”
“” => NULL
“1” => NULL
“1,2” => NULL

翻译:
您将得到一个字符串,其中包含由逗号分隔的字符序列序列。
编写一个函数,返回一个新字符串,该字符串包含除第一个和最后一个字符序列之外的相同字符序列,但这次用空格分隔。
如果输入字符串为空或删除第一项和最后一项将导致结果字符串为空,则返回一个空值(在下面的示例中表示为泛型值NULL)。
解一:

function array(arr){
  return arr.split(",").slice(1,-1).join(" ") || null;
}

解二:

function array(arr){
  arr = arr.split(',')
  arr.pop()
  arr.shift()
  return arr.join(' ') === '' ? null : arr.join(' ')
}
【8 kyu】Holiday VI - Shark Pontoon

Your friend invites you out to a cool floating pontoon around 1km off the beach. Among other things, the pontoon has a huge slide that drops you out right into the ocean, a small way from a set of stairs used to climb out.
As you plunge out of the slide into the water, you see a shark hovering in the darkness under the pontoon… Crap!
You need to work out if the shark will get to you before you can get to the pontoon. To make it easier… as you do the mental calculations in the water you either freeze when you realise you are dead, or swim when you realise you can make it!
You are given 5 variables;
sharkDistance = distance from the shark to the pontoon. The shark will eat you if it reaches you before you escape to the pontoon.
sharkSpeed = how fast it can move in metres/second.
pontoonDistance = how far you need to swim to safety in metres.
youSpeed = how fast you can swim in metres/second.
dolphin = a boolean, if true, you can half the swimming speed of the shark as the dolphin will attack it.
The pontoon, you, and the shark are all aligned in one dimension.
If you make it, return “Alive!”, if not, return “Shark Bait!”.
翻译:
你的朋友邀请你到离海滩约1公里的一个凉爽的漂浮浮筒。除此之外,浮筒上有一个巨大的滑梯,可以让你直接掉入大海,距离一组用来爬出来的楼梯只有一小段距离。
当你从滑梯中跳入水中时,你看到一条鲨鱼在浮筒下面的黑暗中盘旋……该死!
在你到达浮筒之前,你需要弄清楚鲨鱼是否会靠近你。为了让它更容易……当你在水中进行心理计算时,当你意识到自己死了时,你要么会冻僵,要么意识到自己能成功时游泳!
给你5个变量;
sharkDistance=鲨鱼到浮筒的距离。如果鲨鱼在你逃到浮筒之前到达你身边,它会吃掉你。
sharkSpeed=它的移动速度,单位为米/秒。
pontoonDistance=你需要游到安全的距离(米)。
youSpeed=你游泳的速度(米/秒)。
dolphin=布尔值,如果为真,则可以将鲨鱼的游泳速度减半,因为海豚会攻击它。
浮筒、你和鲨鱼都在一个维度上对齐。
如果你成功了,返回“活着!”,如果没有,返回“鲨鱼饵!”。
解:

function shark(pontoonDistance, sharkDistance, youSpeed, sharkSpeed, dolphin){
 return dolphin ? ((sharkDistance/(sharkSpeed/2) > pontoonDistance/youSpeed) ? 'Alive!' : 'Shark Bait!')
                : ((sharkDistance/sharkSpeed > pontoonDistance/youSpeed) ? 'Alive!' : 'Shark Bait!')
}
【7 kyu】Maximum Product

Task
Given an array of integers , Find the maximum product obtained from multiplying 2 adjacent numbers in the array.

Notes
Array/list size is at least 2.

Array/list numbers could be a mixture of positives, negatives also zeroes .

Input >> Output Examples
adjacentElementsProduct([1, 2, 3]); ==> return 6

翻译:
给定一个整数数组,求数组中两个相邻数相乘的最大乘积。
笔记
数组/列表大小至少为2。
数组/列表编号可以是正数、负数和零的混合。
解:

function adjacentElementsProduct(array) {
 var res = []
  for (i = 0; i < array.length - 1; i++) {
    res.push(array[i] * array[i + 1])
  }
  return Math.max(...res)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值