Extending a String prototype - prototypejs

I need to extend de String prototype to build a function that receives a string as parameter and print the same string with a duplicate of the each single character, something like this:
'Boolean'.repeatCharacters
And the output should be:
'BBoolleeaann'
This is my code but I dont understand tottally how extend prototype works:
String.prototype.repeatCharacters = function() {
let strSPlit = String.split('')
let newArr = []
for (let i = 0; i < strSPlit.length; i++) {
newArr.push(strSPlit[i],strSPlit[i])
}
x = newArr.join('')
return x[0]
}
console.log('Boolean'.repeatCharacters())

Related

i don't know why In Leetcode, the result of TestCase and the result of Submit are different

i 'm solving the problem. 139. Word Break. but i have met a problem.
i don't know why the result of TestCase and the result of Submit are different
below is my code,
when i submit my code, it say WrongAnswer for the reason.
Input:
"a"
["b"]
Output:
true
Expected:
false
but when i pressed Run Code, there wasn't problem. I can see Accepted.
also in my VScode Editor, no problem.
i think my code works properly
i don't know why always When submitting code, problem occur.
let Trie = {};
var wordBreak = function (s, wordDict) {
buildTrie(wordDict);
let answer = searchInTrie(s);
return answer;
};
function buildTrie(wordDict) {
for (let i = 0; i < wordDict.length; i++) {
let root = Trie;
const currentWord = wordDict[i];
for (let j = 0; j < currentWord.length; j++) {
if (!root[currentWord[j]]) {
root[currentWord[j]] = {};
}
root = root[currentWord[j]];
}
root["*"] = currentWord;
}
}
function searchInTrie(target) {
let root = Trie;
for (let i = 0; i < target.length; i++) {
const currentChar = target[i];
if (root["*"]) {
root = Trie;
}
// console.log(root, currentChar);
if (!root[currentChar]) {
console.log(root, currentChar);
return false;
}
root = root[currentChar];
}
return true;
}
console.log(root, currentChar);
This statement is adding an extra line of output, which the compiler might consider incorrect. Remove it and then try submitting once.

Cannot successfully compare string values in a Near contract

I am trying to compare string data taken off a Persistant Vector, with an if statement. However, the comparison is not working, it always evaluates to false in the if. The code is below with the comment, //trying to compare strings
export function removeStudent(rmstudidx: i32): string{
let ablist = new Array<string>();
let rmstudent: string = "";
let abstudent: string = "";
let abindx = new Array<i32>();
let absentidx: i32;
let x: i32 = 0;
abstudent = studentlist[rmstudidx];
if(studentlist.containsIndex(rmstudidx)){
rmstudent = studentlist.swap_remove(rmstudidx);
//return "Removed " + rmstudent;
}
while (x < absentlist.length){
abstudent = studentlist[rmstudidx];
if(rmstudent === abstudent){ //trying to compare strings
return "In the string test"
absentlist.swap_remove(x);
x++;
}
}
return "Removed " + rmstudent;
}
In your line, where you write
if(rmstudent === abstudent){
You are writing ===, which is not the same as === in JavaScript.
AssemblyScript is strict, and using === will check if the objects are the same. If you want to do string comparison, you should use == instead.
Here's an example from the documentation of AssemblyScript
var a = "hello"
var b = a
var c = "h" + a.substring(1)
if (a === b) { /* true */ }
if (a === c) { /* false */ }
if (a == c) { /* true */ }

how to to convert for to foreach

jslint tell Unexpected 'for'.
so i think that i must convert for with foreach
but how?
if someone can help
thanks
// Grab the original element
var original = document.getElementsByTagName("noscript")[0];
// Create a replacement tag of the desired type
var replacement = document.createElement("span");
var i;
// Grab all of the original's attributes, and pass them to the replacement
for(i = 0, l = original.attributes.length; i < l; ++i){
var nodeName = original.attributes.item(i).nodeName;
var nodeValue = original.attributes.item(i).nodeValue;
replacement.setAttribute(nodeName, nodeValue);
}
// Persist contents
replacement.innerHTML = original.innerHTML;
// Switch!
original.parentNode.replaceChild(replacement, original);
You have a comma after i = 0, <========
it should be semicolon.
Another issue is declaring l = original.attributes.length you don't need the variable l
just use it as for(i = 0; i < original.attributes.length; ++i){
if you still wanna use a forEach you can do it as:
original.attributes.forEach(element => {
var nodeName = element.nodeName;
var nodeValue = element.nodeValue;
replacement.setAttribute(nodeName, nodeValue);
});
thanks for your answer, i got Uncaught TypeError: original.attributes.forEach is not a function
function Switch() {
var original = document.getElementsByTagName("noscript")[0];
var replacement = document.createElement("span");
original.attributes.forEach(element => {
var nodeName = element.nodeName;
var nodeValue = element.nodeValue;
replacement.setAttribute(nodeName, nodeValue);
});
// Persist contents
replacement.innerHTML = original.innerHTML;
// Switch!
original.parentNode.replaceChild(replacement, original);
}

Refactoring firstReverse using each

// Trying to Refactor the firstReverse function using each? // I Created a func that takes a str as a parameter, use firstReverse within // the for loop the output will be the reversed version of the string.
var stringName = "Matt";
var firstReverse = function(str){ var output = ""; for(var i = str.length -1; i >= 0; i--){ output+= str[i]; } return output; };
firstReverse(stringName)
// Output: "ttaM"
Something like this?
var stringName="Matt";
function firstReverse(str)
{
var newString="";
str.split('').forEach(function(a){newString=a+newString});
return newString;
}
document.getElementById('test')./*danger!!!*/innerHTML = firstReverse(stringName);
<div id="test"></div>

Linq PredicateBuilder

public static IQueryable<SearchProfile> FilterData(string Filter, Repository<SearchProfileContext> dc)
{
IQueryable<SearchProfile> data = null;
var predicate = PredicateBuilder.True<SearchProfile>();
Filter = ExcludedParam(Filter);
if (!string.IsNullOrEmpty(Filter))`enter code here`
{
var stringToSplit = Filter;`enter code here`
List<string[]> arrays = new List<string[]>();
var primeArray = stringToSplit.Split('|');
for (int i = 0; i < primeArray.Length; i++)
{
string first = primeArray[i];
if (first.Contains("chkOrientation") == true)
{
string[] Array = first.Replace("chkOrientation=", "").Split(',');
predicate = predicate.And(a => Array.Contains(a.OrientaionID.ToString()));
}
if (first.Contains("chkProfession") == true)
{
string[] Array = first.Replace("chkProfession=", "").Split(',');
**predicate = predicate.And(a => Array.Contains(SqlFunctions.StringConvert((Double)a.ProfessionID)));**
}
}
data = dc.Select<SearchProfile>().Where(predicate).Distinct();
return data;
}
data = (from a in dc.Select<SearchProfile>().Where(a => a.PersonID > 0) select a).Distinct();
return data;
}
When I ran my program, I got this nasty error below:
LINQ to Entities does not recognize the method Int32 ToInteger(System.Object) method, and this method cannot be translated into a store expression.
then,I used SqlFunctions.StringConvert to make it work but the SQL LINQ generated was not evaluating. This is the sample output (it is comparing '1' and '2' instead of 1 and 2)**
Why are you casting a.ProfessionID to double? What type is a.ProfessionID of?
I think there is implicit conversion to integer, which causes calling the ToInteger method.
And why don't you convert items in Array to integer in the first place, and then use Array of ints in the query?

Resources