Fundamentals
Strings
Logic
Write a function that returns string:
"Fizz" when the number is divisible by 3.
"Buzz" when the number is divisible by 5.
"FizzBuzz" when the number is divisible by 3 and 5.
pub fn fizz_buzz(n: i32) -> String { match (n%3, n%5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (_, _) => n.to_string() } }
public class FizzBuzz{public string GetOutput(int number){if ((number % 3 == 0) && (number % 5 == 0))return "FizzBuzz";else if (number % 3 == 0)return "Fizz";else if (number % 5 == 0)return "Buzz";else return number.ToString();}- pub fn fizz_buzz(n: i32) -> String {
- match (n%3, n%5) {
- (0, 0) => "FizzBuzz".to_string(),
- (0, _) => "Fizz".to_string(),
- (_, 0) => "Buzz".to_string(),
- (_, _) => n.to_string()
- }
- }
#[cfg(test)] mod tests { use crate::fizz_buzz; #[test] fn it_should_return_fizz() { let expected = "Fizz"; let result = fizz_buzz(3); assert_eq!(result, expected); } #[test] fn it_should_return_buzz() { let expected = "Buzz"; let result = fizz_buzz(5); assert_eq!(result, expected); } #[test] fn it_should_return_fizz_buzz() { let expected = "FizzBuzz"; let result = fizz_buzz(15); assert_eq!(result, expected); } #[test] fn it_should_return_args_passed() { let expected = "211"; let result = fizz_buzz(211); assert_eq!(result, expected); } }
namespace Solution {using NUnit.Framework;[TestFixture]public class SolutionTest{[Test]public void GetOutput_InputIsDivisibleBy3_ReturnFizz(){var fizzbuzz = new FizzBuzz();var result = fizzbuzz.GetOutput(3);Assert.That(result, Is.EqualTo("Fizz"));}[Test]public void GetOutput_InputIsDivisibleBy5_ReturnBuzz(){var fizzbuzz = new FizzBuzz();var result = fizzbuzz.GetOutput(5);Assert.That(result, Is.EqualTo("Buzz"));}[Test]public void GetOutput_InputIsDivisibleBy5And3_ReturnFizzBuzz(){var fizzbuzz = new FizzBuzz();var result = fizzbuzz.GetOutput(15);Assert.That(result, Is.EqualTo("FizzBuzz"));}}}- #[cfg(test)]
- mod tests {
- use crate::fizz_buzz;
- #[test]
- fn it_should_return_fizz() {
- let expected = "Fizz";
- let result = fizz_buzz(3);
- assert_eq!(result, expected);
- }
- #[test]
- fn it_should_return_buzz() {
- let expected = "Buzz";
- let result = fizz_buzz(5);
- assert_eq!(result, expected);
- }
- #[test]
- fn it_should_return_fizz_buzz() {
- let expected = "FizzBuzz";
- let result = fizz_buzz(15);
- assert_eq!(result, expected);
- }
- #[test]
- fn it_should_return_args_passed() {
- let expected = "211";
- let result = fizz_buzz(211);
- assert_eq!(result, expected);
- }
- }
pub fn make_uppercase(s: String) -> String { s.to_uppercase() }
function upperCase(s) {//JUST DO IT- pub fn make_uppercase(s: String) -> String {
- s.to_uppercase()
- }
#[cfg(test)] mod tests { use crate::make_uppercase; #[test] fn it_should_equal() { let expected : String = String::from("CODEWARS , HELL YEAH!!"); let result : String = make_uppercase("codewars , hell yeah!!".to_string()); assert_eq!(expected, result); } #[test] fn it_should_not_equal() { let expected : String = String::from("Codewars , Hell Yeah!!"); let result : String = make_uppercase("codewars , hell yeah!!".to_string()); assert_ne!(expected, result); } }
describe("Tests", () => {it("test", () => {Test.assertEquals(upperCase("qwerty"),'QWERTY')});});- #[cfg(test)]
- mod tests {
- use crate::make_uppercase;
- #[test]
- fn it_should_equal() {
- let expected : String = String::from("CODEWARS , HELL YEAH!!");
- let result : String = make_uppercase("codewars , hell yeah!!".to_string());
- assert_eq!(expected, result);
- }
- #[test]
- fn it_should_not_equal() {
- let expected : String = String::from("Codewars , Hell Yeah!!");
- let result : String = make_uppercase("codewars , hell yeah!!".to_string());
- assert_ne!(expected, result);
- }
- }
Create a function that checks if a number n is divisible by two numbers x AND y. All inputs are positive, non-zero digits.
Examples:
- n = 3, x = 1, y = 3 => true because 3 is divisible by 1 and 3
- n = 12, x = 2, y = 6 => true because 12 is divisible by 2 and 6
- n = 100, x = 5, y = 3 => false because 100 is not divisible by 3
- n = 12, x = 7, y = 5 => false because 12 is neither divisible by 7 nor 5
pub fn is_divisible(n: i32, x: i32, y:i32) -> bool { match (n % x == 0, n % y == 0) { (true, true) => true, _ => false } }
function isDivisible(n, x, y) {if (n % x === 0 && n % y === 0) {return true} else {return false}- pub fn is_divisible(n: i32, x: i32, y:i32) -> bool {
- match (n % x == 0, n % y == 0) {
- (true, true) => true,
- _ => false
- }
- }
#[cfg(test)] mod tests { use crate::is_divisible; #[test] fn it_should_return_true() { let expected : bool = true; let result_zero : bool = is_divisible(0, 1, 3); let result_positive : bool = is_divisible(3, 1, 3); let result_negative : bool = is_divisible(-3, 1, 3); assert_eq!(expected, result_zero); assert_eq!(expected, result_positive); assert_eq!(expected, result_negative); } #[test] fn it_should_return_false() { let expected : bool = false; let result_positive : bool = is_divisible(3, 2, 3); let result_negative : bool = is_divisible(-3, 2, 3); assert_eq!(expected, result_positive); assert_eq!(expected, result_negative); } }
const chai = require("chai");const assert = chai.assert;chai.config.truncateThreshold=0;describe("Basic tests", () => {it("Fixed Tests", () => {assert.strictEqual(isDivisible(3,3,4),false);assert.strictEqual(isDivisible(12,3,4),true);assert.strictEqual(isDivisible(8,3,4),false);assert.strictEqual(isDivisible(48,3,4),true);});});- #[cfg(test)]
- mod tests {
- use crate::is_divisible;
- #[test]
- fn it_should_return_true() {
- let expected : bool = true;
- let result_zero : bool = is_divisible(0, 1, 3);
- let result_positive : bool = is_divisible(3, 1, 3);
- let result_negative : bool = is_divisible(-3, 1, 3);
- assert_eq!(expected, result_zero);
- assert_eq!(expected, result_positive);
- assert_eq!(expected, result_negative);
- }
- #[test]
- fn it_should_return_false() {
- let expected : bool = false;
- let result_positive : bool = is_divisible(3, 2, 3);
- let result_negative : bool = is_divisible(-3, 2, 3);
- assert_eq!(expected, result_positive);
- assert_eq!(expected, result_negative);
- }
- }
pub struct User { pub name: String, pub surname: String } impl User { pub fn New(n: String, s: String) -> Self { Self { name: n, surname: s } } }
function User(name, surname){this.name = namethis.surname = surname- pub struct User {
- pub name: String,
- pub surname: String
- }
- impl User {
- pub fn New(n: String, s: String) -> Self {
- Self {
- name: n,
- surname: s
- }
- }
- }
#[cfg(test)] mod tests { use crate::User; #[test] fn it_should_equal() { let expected: User = User { name: "Jeddi".to_string(), surname: "212".to_string() }; let result: User = User::New( "Jeddi".to_string(), "212".to_string() ); assert_eq!(expected.name, result.name); assert_eq!(expected.surname, result.surname); } #[test] fn it_should_equal_2() { let expected: User = User { name: "_Saya".to_string(), surname: "/ex0#h".to_string() }; let result: User = User::New( "_Saya".to_string(), "/ex0#h".to_string() ); assert_eq!(expected.name, result.name); assert_eq!(expected.surname, result.surname); } #[test] fn it_should_not_equal() { let expected: User = User { name: "Name_A".to_string(), surname: "Sur_Name_A".to_string() }; let result: User = User::New( "Name_B".to_string(), "Sur_Name_B".to_string() ); assert_ne!(expected.name, result.name); assert_ne!(expected.surname, result.surname); } }
const { config, assert } = require("chai")config.truncateThreshold = 0describe("Base tests:", function() {it(":^)", function() {assert.deepEqual( new User('Anatoly', 'Wasserman'), { name: 'Anatoly', surname: 'Wasserman' } )assert.deepEqual( new User('React', 'Developer'), { name: 'React', surname: 'Developer' } )})})- #[cfg(test)]
- mod tests {
- use crate::User;
- #[test]
- fn it_should_equal() {
- let expected: User = User { name: "Jeddi".to_string(), surname: "212".to_string() };
- let result: User = User::New( "Jeddi".to_string(), "212".to_string() );
- assert_eq!(expected.name, result.name);
- assert_eq!(expected.surname, result.surname);
- }
- #[test]
- fn it_should_equal_2() {
- let expected: User = User { name: "_Saya".to_string(), surname: "/ex0#h".to_string() };
- let result: User = User::New( "_Saya".to_string(), "/ex0#h".to_string() );
- assert_eq!(expected.name, result.name);
- assert_eq!(expected.surname, result.surname);
- }
- #[test]
- fn it_should_not_equal() {
- let expected: User = User { name: "Name_A".to_string(), surname: "Sur_Name_A".to_string() };
- let result: User = User::New( "Name_B".to_string(), "Sur_Name_B".to_string() );
- assert_ne!(expected.name, result.name);
- assert_ne!(expected.surname, result.surname);
- }
- }