Ad

for loop

function Exponentiation(x)
  local product = 1
  for i = 1, x do
    product = product*x
  end
  return product
end
Code
Diff
  • function bestCodeEver(a)
      return a == 19 and true or false
    end
    
    • def bestCodeEver():
    • bestNumber = 19
    • return bestNumber
    • function bestCodeEver(a)
    • return a == 19 and true or false
    • end
Test Cases
Diff
  • -- TODO: Replace examples and use TDD by writing your own tests
    local solution = require 'solution'
    describe("solution", function()
      it("test for something", function()
        assert.are.same(true, bestCodeEver(19))
        assert.are.same(false, bestCodeEver(15))
      end)
    end)
    
    • import codewars_test as test
    • # TODO Write tests
    • import solution # or from solution import example
    • # test.assert_equals(actual, expected, [optional] message)
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(1 + 1, 2)
    • -- TODO: Replace examples and use TDD by writing your own tests
    • local solution = require 'solution'
    • describe("solution", function()
    • it("test for something", function()
    • assert.are.same(true, bestCodeEver(19))
    • assert.are.same(false, bestCodeEver(15))
    • end)
    • end)

Return a+b where a,b are the inputs to the function

Code
Diff
  • function sum_of_numbers(a, b)
      if a == b then
        return a + a or b + b 
      else
        return math.sqrt(b^2) + math.sqrt(a^2)
      end
    end
      
    • function sum_of_numbers(a, b)
    • return b + a
    • end
    • if a == b then
    • return a + a or b + b
    • else
    • return math.sqrt(b^2) + math.sqrt(a^2)
    • end
    • end
Code
Diff
  • function sum_of_numbers(a, b)
        return b + a
    end
    • def sum_of_numbers(a, b):
    • return b + a
    • function sum_of_numbers(a, b)
    • return b + a
    • end
Test Cases
Diff
  • -- TODO: Replace examples and use TDD by writing your own tests
    local solution = require 'solution'
    describe("solution", function()
      it("test for something", function()
        assert.are.same(2, sum_of_numbers(1,1))
      end)
    end)
    
    • import codewars_test as test
    • # TODO Write tests
    • import solution # or from solution import example
    • # test.assert_equals(actual, expected, [optional] message)
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(1 + 1, 2)
    • -- TODO: Replace examples and use TDD by writing your own tests
    • local solution = require 'solution'
    • describe("solution", function()
    • it("test for something", function()
    • assert.are.same(2, sum_of_numbers(1,1))
    • end)
    • end)