I need to sort it out this quiz [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
my code is not working at all
I need to solve this quiz
question is write convertToString as function !
this function should convert to string from parameter
ex )
let output = convertToString(120);
console.log(output); // --> '120'
let output2 = convertToString('hello');
console.log(output2); // --> 'hello'
let output3 = convertToString(true);
console.log(output3); // --> 'true'
this is what I wrote
function convertToString(anything) {
if (typeof anything === 'number' && typeof anything === 'boolean') {
let ret = anything.toString()
} else {
return anything;
}
return ret1;
}
convertToString(120);

The easiest way to convert anything is by making + operation with ""
function convertToString(anything) {
return "" + anything
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));
console.log(convertToString(null));
console.log(convertToString(undefined));

Zero checks necessary.
function convertToString(val) {
return String(val);
// or return val.toString();
// or return '' + val;
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));

Related

Remove a given character from a string without using replace()? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
Is this the right way to remove a given character from String?
////not using replace method
function removeChar(str1, s) {
let tempĀ  = str1.split('')
let temp2 = []
for (i = 0; i < temp.length; i++) {
if (temp[i] != s) {
temp2.push(temp[i])
}
}
console.log(temp2.join(''));
}
removeChar("Hello","l")
You can do something like this
function removeChar(str1, s) {
return str1.split(s).join('')
}
this is another solution without replace()
const removeChar = (word, letter) => word.split("").filter(v => v !== letter).join("");
Just use the inbuilt replace function of javascript.
newString = oldString.replace(characterToBeReplaced, '');

String as a Single value and Array as multiple value handling [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
If I select one value the I am getting value is like
val : "ABC"
But if I select multiple value my val is comming in array
val : ["ABC","DEF"].
Now because of this problem is neighther I can work on for single value nor multiple. Can anybody tell me how to handle this.
eg:
var myObj = {
data : "M1",
val : "Abc"
}
After adding another value it become
var myObj = {
data : "M2",
val : Abc,Def
}
function handleVal(val){
if ("string" === typeof val){
console.log("Do sth with string");
} else if (Array.isArray(val)){
console.log("Do sth with array")
}
}
handleVal("ABC");
handleVal([1,2])

Test the return value of my function correctly at the time of return [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I can not test the return value of my function correctly at the time of return.
My exercise is this: I must compare two arrays and return true if they have at least one identical element but I need some assistance in figuring out what is wrong with my code:
function duplicateElements(m, n){
function test (element){
return n.includes(element);
}
return m.filter(test) != [] ? true:false;
}
You have to test the length property of the return value.
function duplicateElements(m, n) {
function test(element) {
return n.includes(element);
}
return m.filter(test).length > 0 ? true : false;
}

How to format complex json with javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've been trying to found a simple way to format complex json files and turn it into readable html content.
The goal is to generates a set of nested boxes (html tables) containing the information of my JSON file.
Here is a sample of my json:
{"badgeGroups":
[{"name":"labAccess0", "badges" : ["AAAAAAAAAA"]},
{"name":"labAccess1", "badges" : ["1111111111","29006812B3"]}],
"inputs":
[{"name":"labDoorSecurityButton0", "state":false, "port":12}],
"outputs":
[{"name":"led0","equation":"labDoorSecurityButton0", "state":false, "port":0,"type":"GENERIC", "inputs": ...
Any ideas ?
The answer to your question depends entirely on your expected output formatting.
For example, bloopletech's json2html can parse arbitrary JSON input and a sample output can be seen over here.
If you want more customized formatting there's also json2html jQuery plugin.
All-in-all there are an infinite number of answers to your question as it's simply too broad.
function JSONtoXML(obj) {
var s = "";
var i=0;
s = s + "<" + obj[0];
var test = 0;
for(i=1;i<obj.length;i++) {
if(!(obj[i] instanceof Array) && !(typeof obj[i]==='string')) {
var obje = obj[i];
var k="";
for(prp in obje) {
k = k +" "+ prp + '="';
k = k+ obje[prp] + '" ';
}
s = s+k;
}
else if(!(typeof obj[i]==='string')){
if(test===0) {
s = s+">";
test++;
}
s = s + JSONtoXML(obj[i]);
}
else {
if(test===0) {
s = s+">";
test++;
}
s = s +obj[i];
}
}
if(test===0) {
s = s+">";
test++;
}
s = s+ "</" + obj[0] +">";
return s;
}
This will do the job to convert json to xml.
if you want better readability try some json beautifier.

Javascript variable returning blank [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am testing this code:
var thecol = '';
// Note: I know that value.cat = '3'
if (value.cat === '1') {
thecol = 'cat1';
} else if (value.cat === '2') {
thecol = 'cat2';
} else if (value.cat === '3') {
thecol = 'cat3';
} else if (value.cat === '4') {
thecol = 'cat4';
} else if (value.cat === '5') {
thecol = 'cat5';
};
alert(thecol);
The alert is blank for some reason.
Any ideas why.
Are you sure that value.cat is '3' and not 3? You can easily find out by logging typeof value.cat. Since you are using the deep equal it will only return true if both, the value and the type are the same:
'3' === 3 // -> false
'3' == 3 // -> true

Categories