小编taa*_*bma的帖子

使用if子句在ruby中重复数字

我写了一段代码,false当传递给这个方法的数字中有一个重复的数字时返回.

no_repeat(2114567) #=> false
Run Code Online (Sandbox Code Playgroud)

以下是代码.我找不到它有什么问题.有任何建议,请改善这一点.

def no_repeat(x)
    x = x.to_s.split('')
    i = 0
    while i < x.length
        if x[i].to_s == x[i + 1]
            false
        end 
        i += 1
    end
    true
end

no_repeat(2114567) #=> true
Run Code Online (Sandbox Code Playgroud)

ruby

3
推荐指数
1
解决办法
106
查看次数

基本的循环概念

这是用于评估两个字符串的长度和字符是否相等的正确代码.

function equal(x, y) {
    if(x.length != y.length)
         return false
    var i = 0;
    while(i < x.length) {
       if(x.charAt(i) != y.charAt(i))
          return false
       i++
    }
    return true
  }

equal("hello", "hello") // true
equal("hello", "heyyy") // false
Run Code Online (Sandbox Code Playgroud)

我自己修改了它,我认为这对我来说是合乎逻辑的.但是我的代码(如下所示)在其角色内容不同时返回true.任何人都可以帮我解决我的版本有什么问题吗?

function equal(x, y) {
  if(x.length != y.length)
     return false
  var i = 0;
  while(i < x.length) {
     if(x.charAt(i) != y.charAt(i))
        return false
     else return true
    i++
   }
}

equal("hello", "hi") //false
equal("hello", "heyyy") //true
Run Code Online (Sandbox Code Playgroud)

javascript loops while-loop

1
推荐指数
1
解决办法
76
查看次数

标签 统计

javascript ×1

loops ×1

ruby ×1

while-loop ×1