function greetings(name){
return My name is: ${name}
}
//For greetings(aName); the output would be "My name is aName"
const greetings = (name) => My name is: ${name}
;
//For greetings(anotherName) the output would be "My name is anotherName"
const greetings = (n) => My name is: ${n}
;
//For greetings(thisName); the output would be "My name is thisName"
const greetings = (name) => `My name is: ${name}`;
var myName = 'Elijah'console.log('My name is:'(var myName = 'Elijah'))- const greetings = (name) => `My name is: ${name}`;
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; // Uncomment the following line to disable truncating failure messages for deep equals, do: // chai.config.truncateThreshold = 0; // Since Node 12, we no longer include assertions from our deprecated custom test framework by default. // Uncomment the following to use the old assertions: // const Test = require("@codewars/test-compat"); describe("Solution", function() { it("Basic Tests", function() { assert.strictEqual(greetings("Tom"), "My name is: Tom"); assert.strictEqual(greetings("Sam"), "My name is: Sam"); assert.strictEqual(greetings("Jean"), "My name is: Jean"); }); });
- // Since Node 10, we're using Mocha.
- // You can use `chai` for assertions.
- const chai = require("chai");
- const assert = chai.assert;
- // Uncomment the following line to disable truncating failure messages for deep equals, do:
- // chai.config.truncateThreshold = 0;
- // Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
- // Uncomment the following to use the old assertions:
- // const Test = require("@codewars/test-compat");
- describe("Solution", function() {
it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- it("Basic Tests", function() {
- assert.strictEqual(greetings("Tom"), "My name is: Tom");
- assert.strictEqual(greetings("Sam"), "My name is: Sam");
- assert.strictEqual(greetings("Jean"), "My name is: Jean");
- });
- });
To have the function display whatever name that you want you can write it in the following ways.
function greetings(name){
return `My name is: ${name}`
}
//For greetings(aName); the output would be "My name is aName"
const greetings = (name) => `My name is: ${name}`;
//For greetings(anotherName) the output would be "My name is anotherName"
const greetings = (n) => `My name is: ${n}`;
//For greetings(thisName); the output would be "My name is thisName"
This would make the previous code more flexible if you so desire.
I inserted all the elements inside an arrow funtion which when it's run prompts the for a number. That number is then saved as the main variable of the function which test for even or odds using the modulo of 2, and returning if its odd or even.
const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";
// Explainations for const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";
// This is equivalent to creating a function with an "if statement", starting by declaring the function
Format:
const functionName = (elements) => [conditional statement] ? if true return this : else this
-
"
=>
": Turns the variable into a function, it initiates it. -
"
?
" : The question mark initiat the "if statement" with the condition and the returning values
on each side of it. -
"
:
" : The colon determines what to do if the condition is met or not '[met]:[unmet]'
const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";
const number = prompt("Enter a number: ");if(number % 2 == 0){console.log("The number is even.");}else{console.log("The number is odd.");}- const evenOrOdd = (number = prompt("Enter a number: ")) => (number % 2) == 0 ? "The number is even." : "The number is odd.";