Help with passing test

Joined
Jun 8, 2023
Messages
2
Reaction score
1
i need some help...

function concatenator(a , b) {
return "a and b must be numbers or strings";
}

// 👉 CHALLENGE
// 🧠 concaten ator takes two arguments a and b:
// * If a and b are strings, return them concatenated
// * If a and b are numbers, return their sum
// 🧠 Edge cases:
// * Both a and b must be of the same type (use typeof). Else return the string "a and b must be of the same type"
// * a and b can be either numbers or strings. Else return "a and b must be numbers or strings"

<!-- my test pages returns this with what code i have in there already. -->

CHALLENGE 1 - concatenator
index.html:221 ❌ Test 1 fails: concatenator(1,"a") should return "a and b must be of the same type" but returns "a and b must be numbers or strings"
index.html:221 ❌ Test 2 fails: concatenator("a",1) should return "a and b must be of the same type" but returns "a and b must be numbers or strings"
index.html:220 ✅ Test 3 passes
index.html:220 ✅ Test 4 passes
index.html:221 ❌ Test 5 fails: concatenator("toyota"," camry") should return "toyota camry" but returns "a and b must be numbers or strings"
index.html:221 ❌ Test 6 fails: concatenator(3,5) should return 8 but returns "a and b must be numbers or strings"
index.html:221 ❌ Test 7 fails: concatenator(3,-5) should return -2 but returns "a and b must be numbers or strings"
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
what code i have in there already
And from now somebody have to write it all for you from a blank page, right?
JavaScript:
/*
    concatenator takes two arguments a and b:
    If a and b are strings, return them concatenated
    If a and b are numbers, return their sum
    
    Edge cases:
    Both a and b must be of the same type (use typeof). Else return the string "a and b must be of the same type"
    a and b can be either numbers or strings. Else return "a and b must be numbers or strings"
    */
    
    function concatenator(a, b){
    
    const types = ['number', 'string'];
    let res = '';
    
    switch(true){
    case
        !types.includes( typeof a ) || !types.includes( typeof b )
            : res = 'a and b must be numbers or strings';
                break;
    case
        typeof a !== typeof b
            : res = 'a and b must be of the same type';
                break;
    default
            : res = typeof a == types[0] ? a + b : a + ' ' + b;
                break;               
    }
    
    return res;
    }
    
    console.log( concatenator(1, 'asd') ); // a and b must be of the same type
    console.log( concatenator(1, false) ); // a and b must be numbers or strings
    console.log( concatenator(true, false) ); // a and b must be numbers or strings
    console.log( concatenator(Infinity, 'false') ); // a and b must be of the same type
    console.log( concatenator(Infinity, 0) ); // Infinity
    console.log( concatenator({}, 'false') ); // a and b must be numbers or strings
    console.log( concatenator({}, {}) ); // a and b must be numbers or strings
    
    console.log( concatenator('true', 'false') ); // true false
    console.log( concatenator(3, 5) ); // 8
    console.log( concatenator(2, -5) ); // -3
 
Last edited:
Joined
Nov 24, 2022
Messages
81
Reaction score
7
Code:
function concatenator(a , b) {
if(Number.isInteger(a) && Number.isInteger(b)){return a+b;}
if((Number.isInteger(a) && !Number.isInteger(b)) || (!Number.isInteger(a) && Number.isInteger(b)) )
{return "a and b must be numbers or strings";}

return a.toString()+b.toString();
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top